Find min or max value in a vector in C++
This post will discuss how to find the min or max value in a vector in C++.
1. Using std::max_element
The std::min_element
and std::max_element
return an iterator to the minimum and the maximum value in the specified range, respectively. The following code example shows invocation for both these functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {2, 1, 3, 6, 7, 9, 8}; int max = *max_element(v.begin(), v.end()); int min = *min_element(v.begin(), v.end()); std::cout << min << ", " << max << std::endl; // 1, 9 return 0; } |
Both these functions accept a binary predicate, which can be used to compare a vector of objects using a specific field, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <vector> #include <algorithm> struct Person { std::string name; int age; }; bool comp(Person const &lhs, Person const &rhs) { return lhs.age < rhs.age; } int main() { std::vector<Person> v = { {"A", 10}, {"B", 15}, {"C", 12}, {"D", 14} }; auto min = std::min_element(v.begin(), v.end(), comp); auto max = std::max_element(v.begin(), v.end(), comp); std::cout << "Minimum age object: (" << min->name << ", " << min->age << ")\n"; std::cout << "Maximum age object: (" << max->name << ", " << max->age << ")\n"; return 0; } |
Output:
Minimum age object: (A, 10)
Maximum age object: (B, 15)
2. Using std::minmax_element
A better option is to use the std::minmax_element
function to get the minimum and the maximum elements in a container. It returns a pair of iterators, with the first and second values pointing to the minimum and the maximum element, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {2, 1, 3, 6, 7, 9, 8}; auto it = std::minmax_element(v.begin(), v.end()); int min = *it.first; int max = *it.second; std::cout << min << ", " << max << std::endl; // 1, 9 return 0; } |
To get the index of the elements with the maximum or the minimum value, apply pointer arithmetic, or call to std::distance
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {2, 1, 3, 6, 7, 9, 8}; auto it = std::minmax_element(v.begin(), v.end()); int min_idx = std::distance(v.begin(), it.first); int max_idx = std::distance(v.begin(), it.second); std::cout << min_idx << ", " << max_idx << std::endl; // 1, 5 return 0; } |
3. Using custom routine
Finally, we can write your custom routine to find the minimum and the maximum value in a vector. Here’s what the code would look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <vector> #include <climits> template<typename T> int findMaximum(std::vector<T> const &vec) { int max = INT_MIN; for (const T &i: vec) { if (max < i) { max = i; } } return max; } template<typename T> int findMinimum(std::vector<T> const &vec) { int min = INT_MAX; for (const T &i: vec) { if (min > i) { min = i; } } return min; } int main() { std::vector<int> v = {2, 1, 3, 6, 7, 9, 8}; int min = findMinimum(v); int max = findMaximum(v); std::cout << min << ", " << max << std::endl; // 1, 9 return 0; } |
That’s all about finding the min or max value in a vector in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)