std::minmax_element

来自cppreference.com
< cpp‎ | algorithm

 
 
算法库
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
修改序列操作
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal
修改序列操作
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
分区操作
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
is_partitioned(C++11)
partition
partition_copy(C++11)
排序操作(排序的区间)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
is_sorted(C++11)
is_sorted_until(C++11)
sort
二进制搜索操作(排序的区间)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
设置操作(排序的区间)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
堆的操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小/最大操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
max
max_element
min
min_element
minmax(C++11)
minmax_element(C++11)
数字操作
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
C库
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template< class ForwardIt >

std::pair<ForwardIt,ForwardIt>

    minmax_element( ForwardIt first, ForwardIt last );
(1)
template< class ForwardIt, class Compare >

std::pair<ForwardIt,ForwardIt>

    minmax_element( ForwardIt first, ForwardIt last, Compare comp );
(2)
发现最大和最小的范围内[first, last)元素。使用operator<的值进行比较的第一个版本,第二个版本使用给定的比较函数comp.
Original:
Finds the greatest and the smallest element in the range [first, last). The first version uses operator< to compare the values, the second version uses the given comparison function comp.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 参数

first, last -
前向迭代器定义的范围检查
Original:
forward iterators defining the range to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cmp - comparison function which returns ​true if if *a is less than *b.

The signature of the comparison function should be equivalent to the following:

 bool cmp(const Type1 &a, const Type2 &b);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ​

Type requirements
-
ForwardIt must meet the requirements of ForwardIterator.

[编辑] 返回值

组成的一对迭代器的第一个元素的最小元素和一个迭代的最大元素作为第二。返回std::make_pair(first, first)的范围内是空的。如果几个元素是等价的最小的元素,则返回到第一个这样的元件的迭代器。如果有多个元素的最大元素是等价的,到最后这样的元素,则返回的迭代器.
Original:
a pair consisting of an iterator to the smallest element as the first element and an iterator to the greatest element as the second. Returns std::make_pair(first, first) if the range is empty. If several elements are equivalent to the smallest element, the iterator to the first such element is returned. If several elements are equivalent to the largest element, the iterator to the last such element is returned.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 复杂性

max(floor(3/2(N−1)), 0)应用程序的谓词,N = std::distance(first, last).
Original:
At most max(floor(3/2(N−1)), 0) applications of the predicate, where N = std::distance(first, last).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 可能的实现

First version
template<class ForwardIt>
std::pair<ForwardIt, ForwardIt> 
    minmax_element(ForwardIt first, ForwardIt last)
{
    typedef typename std::iterator_traits<ForwardIt>::value_type value_t;
    return std::minmax_element(first, last, std::less<value_t>());
}
Second version
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt> 
    minmax_element(ForwardIt first, ForwardIt last, Compare comp)
{
    std::pair<ForwardIt, ForwardIt> result(first, first);
 
    if (first == last) return result;
    if (++first == last) return result;
 
    if (comp(*first, *result.first)) {
        result.second = result.first;
        result.first = first;
    } else {
        result.second = first;
    }
    while (++first != last) {
        ForwardIt i = first;
        if (++first == last) {
            if (comp(*i, *result.first)) result.first = i;
            else if (!(comp(*i, *result.second))) result.second = i;
            break;
        } else {
            if (comp(*first, *i)) {
                if (comp(*first, *result.first)) result.first = first;
                if (!(comp(*i, *result.second))) result.second = i;
            } else {
                if (comp(*i, *result.first)) result.first = i;
                if (!(comp(*first, *result.second))) result.second = first;
            }
        }
    }
    return result;
}

[编辑] 为例

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v = { 3, 9, 1, 4, 2, 5, 9 };
 
    auto result = std::minmax_element(v.begin(), v.end());
    std::cout << "min element at: " << (result.first - v.begin()) << '\n';
    std::cout << "max element at: " << (result.second - v.begin()) << '\n';
}

Output:

min element at: 2
max element at: 6

[编辑] 另请参阅

返回区间内的最小元素
(函数模板) [edit]
返回区间内的最大元素
(函数模板) [edit]