
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Maximum Element of a Vector Using STL in C++
Finding the maximum element in a vector is a common task in C++ programming. The Standard Template Library (STL) offers several efficient ways to do this. In this article, we will cover different methods to find the maximum element in a vector in C++. Following is an example of a vector -
For the vector: V = {1, 5, 3, 9, 2} Output: 9 (9 is the maximum element in the vector.) For the vector: V = {22, 23, 5, 6, 34} Output: 34 (34 is the maximum element in the vector.)
Approaches to Find the Maximum Element
We'll cover the following methods for finding the maximum element in a vector.
- Using std::max_element
- Using a Simple Loop
- Using std::sort
- Using std::minmax_element()
- Using a Priority Queue
Using std::max_element
The simplest and most commonly used approach is using std::max_element. This function finds the maximum element in a range and gives you a pointer to it.
Example
Here's the C++ code to find the maximum value using std::max_element. We give std::max_element the start and end of the vector, and it returns an iterator to the maximum element. Then, we use this iterator to get the maximum value.
#include <iostream> #include <vector> #include <algorithm> // For std::max_element using namespace std; int main() { vector<int> v = {1, 5, 3, 9, 2}; // Use std::max_element to get an iterator to the maximum element auto maxElem = *max_element(v.begin(), v.end()); // Dereference to get the value cout << "Maximum element: " << maxElem << endl; return 0; }
Output
The output of the above program is as follows -
Maximum element: 9
Using a Simple Loop
We can also find the maximum element by going through the vector and comparing each element to keep track of the maximum one.
Example
In this method, we start by assuming the first element is the maximum. Then, we loop through the rest of the vector and update the maximum whenever we find a larger element.
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {1, 5, 3, 9, 2}; // Start with the first element as the maximum int maxElem = v[0]; // Loop through the vector to find the maximum element for (int i = 1; i < v.size(); i++) { if (v[i] > maxElem) { // Update maxElem if we find a larger element maxElem = v[i]; } } cout << "Maximum element: " << maxElem << endl; return 0; }
Output
The output of the above program is as follows -
Maximum element: 9
Using std::sort
Another way to find the maximum element is by sorting the vector in descending order. Once sorted, the first element will be the maximum.
Example
In this example, we use std::sort() to sort the vector in descending order. After sorting, we access the first element, which will be the maximum.
#include <iostream> #include <vector> #include <algorithm> // For std::sort using namespace std; int main() { vector<int> v = {1, 5, 3, 9, 2}; // Sort the vector in descending order sort(v.begin(), v.end(), greater<int>()); // The first element is the maximum cout << "Maximum element: " << v[0] << endl; return 0; }
Output
The output of the above program is as follows -
Maximum element: 9
Using std::minmax_element()
The std::minmax_element() function helps find both the minimum and maximum elements in a container. It returns a pair of iterators: one points to the minimum element, and the other points to the maximum element.
Example
In this example, we use std::minmax_element to get the iterators for both the minimum and maximum elements, and we print the maximum value by accessing the value pointed to by the second iterator.
#include <iostream> #include <vector> #include <algorithm> // For std::minmax_element using namespace std; int main() { vector<int> v = {1, 5, 3, 9, 2}; // Get both min and max elements using minmax_element auto result = minmax_element(v.begin(), v.end()); // Dereference the second iterator for the max cout << "Maximum element: " << *result.second << endl; return 0; }
Output
The output of the above program is as follows:
Maximum element: 9
Using a Priority Queue
A priority queue is a container that arranges elements so that the maximum (or minimum) element can be accessed easily. In C++, the default priority queue is a max-heap, which means the maximum element is always at the top.
Example
We use a priority queue here to store the elements of the vector. Since the priority queue keeps the maximum element at the top, we can directly access the maximum element using the top() function.
#include <iostream> #include <vector> #include <queue> // For priority_queue using namespace std; int main() { vector<int> v = {1, 5, 3, 9, 2}; // Create a max-heap (priority queue) priority_queue<int> pq(v.begin(), v.end()); // The top element is the maximum cout << "Maximum element: " << pq.top() << endl; return 0; }
Output
The output of the above program is as follows:
Maximum element: 9
Conclusion
In this article, we covered different ways to find the maximum element in a vector in C++, including std::max_element, a simple loop, std::sort, std::minmax_element(), and a priority queue. Each method has its benefits, and knowing them helps you pick the best one for your situation.