std::minmax_element
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class ForwardIt > std::pair<ForwardIt,ForwardIt> |
(1) | |
template< class ForwardIt, class Compare > std::pair<ForwardIt,ForwardIt> |
(2) | |
发现最大和最小的范围内
[first, last)
元素。使用operator<的值进行比较的第一个版本,第二个版本使用给定的比较函数comp
.原文:
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
.目录 |
[编辑] 参数
first, last | - | 前向迭代器定义的范围检查
原文: forward iterators defining the range to examine |
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. |
类型要求 | ||
-ForwardIt 必须满足 ForwardIterator 的要求。
|
[编辑] 返回值
组成的一对迭代器的第一个元素的最小元素和一个迭代的最大元素作为第二。返回std::make_pair(first, first)的范围内是空的。如果几个元素是等价的最小的元素,则返回到第一个这样的元件的迭代器。如果有多个元素的最大元素是等价的,到最后这样的元素,则返回的迭代器.
原文:
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.
[编辑] 复杂度
最max(floor(3/2(N−1)), 0)应用程序的谓词,N = std::distance(first, last).
原文:
At most max(floor(3/2(N−1)), 0) applications of the predicate, where N = std::distance(first, last).
[编辑] 可能的实现
版本一 |
---|
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>()); } |
版本二 |
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'; }
输出:
min element at: 2 max element at: 6
[编辑] 另请参阅
返回区间内的最小元素 (函数模板) | |
返回区间内的最大元素 (函数模板) |