How to Find the Maximum Element of a Vector using STL in C++?
Last Updated :
02 Nov, 2024
Given a vector, find the maximum element of the vector using STL in C++.
Example
Input: v = {2, 4, 1, 5, 3}
Output: 5
Explanation: 5 is the largest element of vector.
Input: v = {11, 23, 3, 5, 24}
Output: 24
Explanation: 24 is the largest element of the given range.
STL provides the following different methods to find the maximum element in the vector in C++.
Using std::max_element()
The simplest way to find the maximum element in the vector is by using the std::max_element(). This function returns the iterator to the maximum element in the given range of elements.
Syntax
std::max_element(v.begin(), v.end());
where, v is the vector.
Example
C++
// C++ program to find maximum element
// in vector using std::max_element()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {2, 4, 1, 5, 3};
// Finding the maximum element in vector
// using std::max_element()
cout << *max_element(v.begin(), v.end());
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Auxiliary Space: O(1)
Using std::minmax_element()
The std::minmax_element() function can be used to find the maximum and the minimum element in the STL container at once. It returns a pair in which pair::first is minimum, and pair::second is maximum.
Syntax
std::minmax_element(v.begin(), v.end());
where, v is the vector.
Example
C++
// C++ Program to find maximum element in vector
// using std::minmax_element()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {2, 4, 1, 5, 3};
// Finding both minimum and maximum elements
auto p = minmax_element(v.begin(), v.end());
// Printing the maximum element
cout << *p.second;
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Auxiliary Space: O(1)
Using Priority Queue
We can also find the maximum element in a vector by copying all elements of vector into std::priority_queue as it implements the max heap in which the maximum element will always be at the top. This method is useful when we want to find the maximum element between frequent insertions and deletions.
Example
C++
// C++ program to find maximum element in
// vector using priority_queue
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {2, 4, 1, 5, 3};
// Copying all elements of vector v into
// priority queue
priority_queue<int> pq(v.begin(), v.end());
// Fetching the top element which is the
// maximum element of vector
cout << pq.top();
return 0;
}
Time Complexity: O(n), where n is the number of elements in the vector.
Auxiliary Space: O(n)
Using std::sort()
In a vector sorted in ascending order, the maximum element is at end of the vector. So, we sort vector using std::sort() function and find the maximum element.
Example
C++
// C++ Program to find maximum element
// of a vector by sorting
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {2, 4, 1, 5, 3};
// Sort the vector in ascending order
sort(v.begin(), v.end());
// maximum element at the end
cout << v.back();
return 0;
}
Time Complexity: O(n * log n), where n is the number of elements in the vector.
Auxiliary Space: O(log n)
Similar Reads
How to Find the Minimum and Maximum Element of a Vector Using STL in C++? In this article, we will learn how to find the minimum and maximum element in vector in C++.The simplest method to find the minimum and maximum element in vector is by using min_element() and max_element(). Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std;
2 min read
How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo
3 min read
How to Find Minimum Element in a Vector in C++? Given a vector of n elements, the task is to find the minimum element using C++.ExamplesInput: v = {2, 4, 1, 5, 3}Output: 1Explanation: 1 is the smallest element in the vectorInput: v = {12, 34, 5, 7}Output: 5Explanation: 5 is the smallest element in the vector.Following are the 6 different methods
4 min read
How to Find Minimum and Maximum Element of an Array Using STL in C++? Given an array of n elements, the task is to find the minimum and maximum element of array using STL in C++.ExamplesInput: arr[] = {1, 45, 54, 7, 76}Output: min = 1, max = 76Explanation: 1 is the smallest and 76 is the largest among all elements.Input: arr[] = {10, 7, 5, 4, 6}Output: min = 4, max =
3 min read
How to Find the Second Occurrence of an Element in Vector in C++? In C++, vector containers store the data in a contiguous memory location like arrays and also can resize themselves to store more elements. In this article, we will learn how to find the second occurrence of a specific element in a vector in C++. Example Input: myVector = {20, 30, 10, 50, 10, 80, 10
2 min read
How to Find the Maximum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to find the maximum element in a list in C++. Example: Input: myList = {30, 20, 10, 5
2 min read
How to Find the Maximum Element in a Deque in C++? in C++, double-ended queues, also known as deques, are sequence containers with the feature of insertion and deletion on both ends. They are similar to vectors but are more efficient for the insertion and deletion of elements from both ends. In this article, we will learn how to find the maximum ele
2 min read
How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s
2 min read
How to Access First Element in a Vector Using Iterator in C++? In C++, vectors are containers similar to arrays but unlike arrays, vectors can resize themselves during the runtime. These vectors provide iterators to access their elements. In this article, we will learn how to access the first element in a vector using an iterator in C++. Example Input:myVector
2 min read
How to Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int
2 min read