C++ Algorithm min()C++ Algorithm min() function can be used in following 3 ways:
Elements are compared using operator< for the first version or using the given binary comparison function comp for the second version. SyntaxParametera: first value to compare. b: Second value to compare. 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. il: An initializer_list with the values to compare. Return valueIt returns the smaller of a and b. If the values are equivalent, returns a. Returns the smallest value in il. If several values are equivalent to the minimum, returns the left most such value. 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 demonstrate the use of min(): Output: smaller of 1 and 9999: 1 smaller of 'a', and 'b': a shortest of "foo", "bar", and "hello": foo Example 2Let's see another simple example to demonstrate the use of min() using default version: Output: min(1,2)==1 min(2,1)==1 min('a','z')==a min(3.14,2.72)==2.72 Example 3Let's see another simple example to demonstrate the use of min() using predicate function comp: Output: 5 7 Example 4Let's see a simple example to find the minimum element in the list: Output: smallest element in the list is: -1
Next TopicC++ Algorithm
|