STL algorithms
The problem with std::max
and std::min
is if we want to get the max/min value of more than two numbers, we need to use the initializer list version that doesn't work with containers like std::vector
.
Thankfully, there are std::max_element
and std::min_element
for this purpose. Unlike std::max
and std::min
, they return an iterator to the value, not the value itself. To get the value pointed by the iterator, we can the de-reference *
operator:
auto it = std::max_element(v.begin(), v.end());
std::cout << *it << std::endl;
Use std::max_element and std::min_element if the size of the container is unknown.
Write a program which takes multiple integers and prints out the value of the maximum and minimum elements in a line each.
std::cin
inside a while loop.std::max_element
and std::min_element
.*
.