# C++ Algorithm minmax_element()

C++ Algorithm minmax_element() function is used to return a pair with an iterator pointing to the smallest value in the range[ first, last) as first element, and largest as second.

If several values are equivalent to smaller, the first iterator points to the first of such values and several values are equivalent to larger, the second iterator points to the last of such values.

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

## Parameter

first: An input iterator that indicates the first position of the range to compare.

last: An input iterator that indicates the past last element in the range to compare.

comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order otherwise, false. 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 value as first element and larger value as second element in the range [first, last).

If several values are equivalent to smaller, the first iterator points to the first of such values and several values are equivalent to larger, the second iterator points to the last of such values.

## Complexity

Complexity is linear in 1.5 times one less than the number of elements compared.

## Data races

The objects in the range [first, last) are accessed.

## Exceptions

This function throws an exception if any comparison throws an exception.

## Example 1

Let's see the simple example to demonstrate the use of minmax_element():

Output:

```min element at: 2
max element at: 6
```

## Example 2

Let's see another simple example to demonstrate the use of minmax_element() using default version:

Output:

```min is 2, at position 2
max is 9, at position 3
```

## Example 3

Let's see another simple example to demonstrate the use of minmax_element() using comparison function:

Output:

```The minimum value position obtained is : 1
The maximum value position obtained is : 0

The minimum value position obtained is : 1
The maximum value position obtained is : 5
```

Next TopicC++ Algorithm

### Feedback

• Send your Feedback to feedback@javatpoint.com