How to find the maximum element of a Vector using STL in C++?


In this tutorial, we will be discussing a program to understand how to find the maximum element of a vector using STL in C++.

To find the maximum element from a given vector, we will be using the *max_element() method from STL library.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //defining the vector
   vector<int> a = { 1, 45, 54, 71, 76, 12 };
   cout << "Vector: ";
   for (int i = 0; i < a.size(); i++)
      cout << a[i] << " ";
   cout << endl;
   //finding the maximum element
   cout << "Max Element = " << *max_element(a.begin(), a.end());
   return 0;
}

Output

Vector: 1 45 54 71 76 12
Max Element = 76

Updated on: 25-Feb-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements