Movatterモバイル変換


[0]ホーム

URL:


cplusplus.com

Reference

<algorithm>

function template
<algorithm>

std::minmax_element

default (1)
template <class ForwardIterator>  pair<ForwardIterator,ForwardIterator>    minmax_element (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>  pair<ForwardIterator,ForwardIterator>    minmax_element (ForwardIterator first, ForwardIterator last, Compare comp);
Return smallest and largest elements in range
Returns apair with an iterator pointing to the element with the smallest value in the range[first,last) as first element, and the largest as second.

The comparisons are performed using eitheroperator< for the first version, orcomp for the second.

If more than one equivalent element has the smallest value, the first iterator points to the first of such elements.

If more than one equivalent element has the largest value, the second iterator points to the last of such elements.

Parameters

first, last
Input iterators to the initial and final positions of the sequence to compare. The range used is[first,last), which contains all the elements betweenfirst andlast, including the element pointed byfirst but not the element pointed bylast.
comp
Binary function that accepts two elements in the range as arguments, and returns a value convertible tobool. The value returned indicates whether the element passed as first argument is considered less than the second.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return value

Apair with an iterator pointing to the element with the smallest value in the range[first,last) as first element, and the largest as second.

pair is a class template defined in<utility>.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// minmax_element#include <iostream>// std::cout#include <algorithm>// std::minmax_element#include <array>// std::arrayint main () {  std::array<int,7> foo {3,7,2,9,5,8,6};auto result = std::minmax_element (foo.begin(),foo.end());// print result:  std::cout <<"min is " << *result.first;  std::cout <<", at position " << (result.first-foo.begin()) <<'\n';  std::cout <<"max is " << *result.second;  std::cout <<", at position " << (result.second-foo.begin()) <<'\n';return 0;}

Output:
min is 2, at position 2max is 9, at position 3


Complexity

Up to linear in1.5 times one less than the number of elements compared.

Data races

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

Exceptions

Throws if any comparison throws.
Note that invalid arguments causeundefined behavior.

See also

minmax
Return smallest and largest elements(function template)
min_element
Return smallest element in range(function template)
max_element
Return largest element in range(function template)
lower_bound
Return iterator to lower bound(function template)
upper_bound
Return iterator to upper bound(function template)
Home page |Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved -v3.3.4s
Spotted an error? contact us

[8]ページ先頭

©2009-2026 Movatter.jp