C++ Algorithm min_element()
C++ Algorithm min_element() returns an iterator pointing to the element with the smallest value in the range [first, last).
Elements are compared using operator< for the first version or using the given binary comparison function comp for the second version.
Syntax
Parameter
first: An input iterator pointing the position to the first element in the range to be searched for the smallest element.
last: An input iterator pointing the position to the past last element in the range to be searched for the smallest element.
comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements.
Return value
It returns the smaller value in the range [first, last) or last if the range is empty.
Complexity
Complexity is linear in one less than the number of elements compared.
Exceptions
This function throws an exception if any comparison throws an exception.
Please note that invalid parameters cause an undefined behavior.
Example 1
Let’s see the simple example to find the position of smallest element in the range:
Output:
min element at: 3
Example 2
Let’s see another simple example to find the smallest element in the range using default version:
Output:
Minimum element is: 2
Example 3
Let’s see another simple example to find the smallest element in the range using comparison function:
Output:
Smallest element is: 1
Example 4
Let’s see another simple example:
Output:
min_element() without predicate Vector : 40 80 50 30 10 70 60 100 20 90 Minimum element = 10 min_element() with predicate NameMarks Ajeet7 Deep5 Nikita10 Soonu2 Person with best rank = Soonu, Rank = 2