Maximum and Minimum Value From a Vector in C++

Shikha Chaudhary Oct 12, 2023
  1. Use a for Loop to Find the Maximum and Minimum Value From a Vector in C++
  2. Use the std::max_element and the std::min_element Functions to Get the Maximum and Minimum Value From a Vector in C++
  3. Use the std::minmax_element Function to Get the Maximum and Minimum Values From a Vector in C++
Maximum and Minimum Value From a Vector in C++

A vector in C++ is nothing but a dynamic array that can resize itself automatically. Since a vector is a container for elements, we may want to find out the maximum or the minimum value that a vector contains.

While dealing with vectors, we can use a loop just like we do when searching for the maximum or the minimum element in an array. This article will also look at some library functions that help us do the same.

Use a for Loop to Find the Maximum and Minimum Value From a Vector in C++

In the example below, we have two function templates; maxElement for finding the maximum element and minElement for finding the minimum element. Inside the main() block, we define a vector, marks and pass it to the template functions.

The value returned by the minElement function and maxElement function is stored in the variables, min, and max, respectively, and printed these variables to the screen. The maxElement function has a variable max, which contains the macro INT_MIN value, and then we use the for loop to go over all the vector elements and compare them to the value stored in max.

We update the value of max if we find an element greater than the value stored in it, and is done for all of the vector’s elements. The function maxElement returns the last updated value of the variable max, and this is how we get the maximum value from the vector marks.

The minElement function also works in the same way and returns the minimum value of the vector. Let’s have an example of using a loop to find the maximum and minimum value from a vector in C++.

Code:

#include <climits>
#include <iostream>
#include <vector>

using namespace std;

template <typename D>
int maxElement(vector<D> const &v) {
  int max = INT_MIN;
  for (const D &i : v) {
    if (max < i) {
      max = i;
    }
  }
  return max;
}

template <typename D>
int minElement(vector<D> const &v) {
  int min = INT_MAX;
  for (const D &i : v) {
    if (min > i) {
      min = i;
    }
  }
  return min;
}

int main() {
  vector<int> marks = {23, 45, 65, 23, 43, 67, 87, 12};

  int min = minElement(marks);
  int max = maxElement(marks);

  cout << "The minimum marks are: " << min << endl;
  cout << "The maximum marks are: " << max << endl;

  return 0;
}

Output:

The minimum marks are: 12
The maximum marks are: 87

This is an easy approach to finding the maximum and minimum elements from a vector in C++.

Use the std::max_element and the std::min_element Functions to Get the Maximum and Minimum Value From a Vector in C++

To find the maximum and minimum values from a vector in C++, we can use the std::max_element and std::min_element functions respectively.

The max_element function returns an iterator that points to the largest value, and the min_element function returns an iterator that points to the smallest value, both in the range (start, end).

Syntax:

*min_element(start_index, end_index);
*max_element(start_index, end_index);

In the following example, we first pass the pointers to the start and end of the vector, marks to both the functions, max_element, and min_element as arguments. The values returned by the max_element and min_element functions are stored in the variables max and min, respectively.

Code:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> marks = {23, 34, 56, 75, 23, 44, 58};

  int max = *max_element(marks.begin(), marks.end());
  int min = *min_element(marks.begin(), marks.end());

  cout << "The minimum marks are: " << min << endl;
  cout << "The maximum marks are: " << max << endl;

  return 0;
}

Output:

The minimum marks are: 23
The maximum marks are: 75

Note that both these functions can be used with the help of a pre-defined function for doing comparisons. Read more about these functions here.

Use the std::minmax_element Function to Get the Maximum and Minimum Values From a Vector in C++

The std::minmax_element is more like a condensed version of the above two functions. Instead of separately using them, we can use the std::minmax_element function to get a pair of iterators as the returned value.

This function will return a pair of iterators where the first value points to the minimum element and the second one to the maximum element. For example, we pass the first and last index of the vector, marks, to the function minmax_element, and the value returned by this function is stored in the variable, res, which is defined with the auto keyword.

Then, we use the dot(.) operator to separate the minimum and maximum values from the pair of iterators and store them in the variables, min and max. We use the minmax_element function to get the maximum and minimum values from a vector in C++.

Code:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> marks = {34, 23, 56, 75, 23, 67, 88, 12};

  auto res = minmax_element(marks.begin(), marks.end());
  int min = *res.first;
  int max = *res.second;

  cout << "The minimum marks are: " << min << endl;
  cout << "The maximum marks are: " << max << endl;

  return 0;
}

Output:

The minimum marks are: 12
The maximum marks are: 88

Below is the modified version of the above code that also returns the index of the maximum and minimum value of the vector. This is done using the std::distance function, which calculates the number of elements between the first and last elements.

Code:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> marks = {34, 23, 56, 75, 23, 67, 88, 12};

  auto res = minmax_element(marks.begin(), marks.end());
  int min = *res.first;
  int max = *res.second;

  int indx_max = distance(marks.begin(), res.first);
  int indx_min = distance(marks.begin(), res.second);

  cout << "The minimum marks are: " << min << endl;
  cout << "Found at index: " << indx_min << endl;

  cout << "The maximum marks are: " << max << endl;
  cout << "Found at index: " << indx_max << endl;

  return 0;
}

Output:

The minimum marks are: 12
Found at index: 6
The maximum marks are: 88
Found at index: 7

To learn more about this function, check this documentation. This is all about how we can get the maximum and minimum value from a vector in C++.

Related Article - C++ Vector