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. SyntaxParameterfirst: 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 valueIt returns the smaller value in the range [first, last) or last if the range is empty. ComplexityComplexity is linear in one less than the number of elements compared. ExceptionsThis function throws an exception if any comparison throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to find the position of smallest element in the range: Output: min element at: 3 Example 2Let's see another simple example to find the smallest element in the range using default version: Output: Minimum element is: 2 Example 3Let's see another simple example to find the smallest element in the range using comparison function: Output: Smallest element is: 1 Example 4Let'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 Name Marks Ajeet 7 Deep 5 Nikita 10 Soonu 2 Person with best rank = Soonu, Rank = 2
Next TopicC++ Algorithm
|