# C++ Algorithm min()

C++ Algorithm min() function can be used in following 3 ways:

1. It compares the two values passed in its arguments and returns the smaller between them, and if both are equal, then it returns the first one.
2. It also compares the two values using a binary function, which is defined by the user, and then passed as argument in std::min().
3. It is also used to find the smallest element in a given list, and it returns the first one if there are more than one are smallest in the list.

Elements are compared using operator< for the first version or using the given binary comparison function comp for the second version.

## Parameter

a: 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 value

It 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.

## 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 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 2

Let'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 3

Let's see another simple example to demonstrate the use of min() using predicate function comp:

Output:

```5
7
```

## Example 4

Let's see a simple example to find the minimum element in the list:

Output:

```smallest element in the list is: -1
```

Next TopicC++ Algorithm

### Feedback   