Minimum & Maximum
Lesson 22 of 23 in Coddy's C++ - Standard Template Library course.
In the C++ STL there are methods that help us find the minimum and maximum between two elements, between a sequence of elements and so on.
In this lesson you'll learn about four methods:
min()&max()min_element&max_element()
min() method
This method returns the smaller element among two elements, a and b.
int a = 10, b = 20;
cout << min(a, b);Output:
10max() method
This method returns the larger element among two elements, a and b.
int a = 10, b = 20;
cout << max(a, b);Output:
20min_element() method
This method returns the smallest element in the range defined by two iterators. It returns it's iterator, so if we want to output it to the screen we will need to use the * operator.
vector<int> numbers = {5, 10, 3, 1, 7, 20};
cout << *min_element(numbers.begin(), numbers.end());Output:
1max_element() method
This method returns the largest element in the range defined by two iterators. It returns it's iterator, so if we want to output it to the screen we will need to use the * operator.
vector<int> numbers = {5, 10, 3, 1, 7, 20};
cout << *max_element(numbers.begin(), numbers.end());Output:
20
Try it yourself
This lesson doesn't include a code challenge.