Maximum Value in Vector in C++
A vector can store items of similar data types. We can expand the vector’s size dynamically. When running the program, it's possible to scale the vector. It is similar to arrays, but the array size cannot be changed once initialized.
In C++, the vector header file is used to access vectors. Standard Template Library is the package that includes vectors.
Syntax of Vector
#include <vector>
std::vector <par> vec_name;
where,
- par is the data type of the vector
- vec_name is the vector name.
Ways to Find Maximum Value in a Vector
There are different methods to find maximum values in a vector.
- max_element
- minmax_element
- custom routine
1. max_element
This is the most simple and widely used method to find the maximum value in the vector. A function named std::max finds the maximum element out of 2 or more. But we want to search for the maximum element from the vector, so we use std::max_element in C++. In this function, we make a vector consisting of multiple elements, and then it gives back an iterator that points to the maximum element between the first and last values.
Syntax
max_element(start, end);
In this, the function max_element is passed with two parameters, start and end, which indicates the range of the vector, that is, the list or range from which the maximum element is to be found.
- First: the iterator that points to the first value of the range in the vector
- Last: the iterator that points to the last value of the range of the vector
It will return an iterator that points to the maximum value of the vector.
Example 1: Program to understand max_element in C++
CODE
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec = {5, 1, 6, 0, 7, 9, 8};
int maximum = *max_element(vec.start(), vec.end());
std::cout << "The maximum value is: " << maximum << std::endl;
return 0;
}
OUTPUT
![Maximum Value in Vector in C++](https://static.tutorialandexample.com/cpp/maximum-value-in-vector-in-cpp1.png)
- We can use a comparator with std::max_element
It is a pre-defined function that takes two elements in the range and returns the maximum value.
Syntax
max_element(start, end, Compr);
In this, there are 3 parameters:
- Start: the iterator that points to the first value of the range in the vector
- End: the iterator that points to the last value of the range of the vector
- Comp: it takes elements in the range and returns a value after converting to bool.
It will return an iterator that points to the maximum value of the vector.
Example 2: Program to understand max_element with comparator in C++.
CODE
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec = {5, 1, 6, 0, 7, 9, 8};
int maximum = *max_element(vec.start(), vec.end(), comp);
std::cout << "The maximum value is: " << maximum << std::endl;
return 0;
}
OUTPUT
![Maximum Value in Vector in C++](https://static.tutorialandexample.com/cpp/maximum-value-in-vector-in-cpp2.png)
2. minmax_element
Another method to find the maximum element in the vector is to use std::minmax_element. This is an in-built function that gets the largest elements in a container. It returns a pair of iterators with two values, minimum and maximum element, which is pointed by the first and second value.
Syntax
minmax_element(start, end);
This function takes 2 parameters:
- Start: the first value of the range
- End: the last value of the range
Example 3: Program to understand minmax_element in C++.
CODE
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec = {5, 1, 6, 0, 7, 9, 8};
int maximum = *minmax_element(vec.start(), vec.end());
std::cout << "The maximum value is: " << maximum << std::endl;
return 0;
}
OUTPUT
![Maximum Value in Vector in C++](https://static.tutorialandexample.com/cpp/maximum-value-in-vector-in-cpp3.png)
To findthe index of the maximum element in the vector, we can call the std::distance method or use the arithmetic function on the pointer.
Example 4: To find the index of the maximum element in C++.
CODE
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec = {2, 7, 3, 6, 0, 9, 8};
auto ind = std::minmax_element(v.begin(), v.end());
int max_index = std::distance(v.begin(), ind.second);
std::cout << "The index of maximum value is " << max_index << std::endl;
return 0;
}
OUTPUT
![Maximum Value in Vector in C++](https://static.tutorialandexample.com/cpp/maximum-value-in-vector-in-cpp4.png)
3. Custom Routine
Another method to find the maximum value in the vector is using the custom routine.
Syntax
template <typename temp>
int Func_name(std::vector <temp> const &vector_name)
{
//statements
}
Firstly, we have to make a custom vector using a template. Then we made a function in which there are two parameters, calling a vector with its name and the template name. We can make the custom vector under the <climits> header file. We can also make a class instead of a function and can call it by making an object.
Example 5: Finding the maximum value in the vector using a custom routine in C++.
CODE
#include <iostream>
#include <vector>
#include <climits>
template<typename temp>
int find_Maximum(std::vector<temp> const &v) {
int maximum = INT_MIN;
for (const temp &i: v) {
if (maximum < i) {
maximum = i;
}
}
return max;
}
int main()
{
std::vector<int> v = {2, 1, 3, 6, 7, 9, 8};
int max = find_Maximum(v);
std::cout <<"The maximum value is " << max << std::endl; // 1, 9
return 0;
}
OUTPUT
![Maximum Value in Vector in C++](https://static.tutorialandexample.com/cpp/maximum-value-in-vector-in-cpp5.png)