简体   繁体   English

C ++ STL中max_element和minmax_element的行为差异

[英]Difference in behaviour of max_element and minmax_element in C++ STL

In C++ max_element , if there are multiple elements which are the maximum, it returns the first such element. 在C ++ max_element ,如果有多个元素是最大元素,则返回第一个这样的元素。 Whereas minmax_element (C++11 onwards) returns the last max element. minmax_element (C ++ 11以上)返回最后一个max元素。

Is there a reason from standards for this behaviour? 这种行为的标准是否有原因?

From cplusplus.com 来自cplusplus.com

If more than one equivalent element has the largest value, the second iterator points to the last of such elements. 如果多个等效元素具有最大值,则第二个迭代器指向最后一个这样的元素。

The comparisons are performed using either operator< for the first version, or comp for the second; 比较是使用operator <作为第一个版本,或comp作为第二个版本; An element is the largest if no other element does not compare less than it. 如果没有其他元素不比它少,则元素是最大的。 If more than one element fulfills this condition, the iterator returned points to the first of such elements. 如果多个元素满足此条件,则迭代器返回指向第一个这样的元素。

Boost's documentation of their library includes the rationale Boost的库文档包括基本原理

The definition problem surfaces when one tries to design a minmax_element, using the procedure proposed in (Cormen, Leiserson, Rivest: "Introduction to Algorithms", section 9.1). 当人们尝试使用(Cormen,Leiserson,Rivest:“算法导论”,第9.1节)中提出的程序设计minmax_element时,定义问题浮出水面。 It should be possible to derive an algorithm using only 3n/2 comparisons if [first,last) has n elements, but if one tries to write a function called first_min_first_max_element() which returns both std::min_element and std::max_element in a pair, the trivial implementation does not work. 如果[first,last]有n个元素,但是如果有人试图编写一个名为first_min_first_max_element()的函数来返回std :: min_element和std :: max_element,那么应该可以仅使用3n / 2比较来推导算法。对,琐碎的实现不起作用。 The problem, rather subtly, is about equal elements: I had to think for a while to find a way to perform only three comparisons per pair and return the first min and first max elements. 问题,相当巧妙,是关于相同的元素:我不得不考虑一段时间找到一种方法,每对只执行三次比较,并返回第一个最小和第一个最大元素。 For a long time, it seemed any attempts at doing so would consume four comparisons per pair in the worst case. 很长一段时间,似乎任何尝试这样做都会在最坏的情况下每对消耗四次比较。 This implementation achieves three. 该实现实现了三个。

It is not possible (or even desirable) to change the meaning of max_element, but it is still beneficial to provide a function called minmax_element, which returns a pair of min_element and max_element. 改变max_element的含义是不可能的(甚至是不可取的),但提供一个名为minmax_element的函数仍然是有益的,它返回一对min_element和max_element。 Although it is easy enough to call min_element and max_element, this performs 2(n-1) comparisons, and necessitates two passes over the input. 尽管调用min_element和max_element很容易,但这会执行2(n-1)次比较,并且需要对输入进行两次传递。 In contrast, minmax_element will perform the fewer comparisons and perform a single pass over the input. 相反,minmax_element将执行较少的比较并对输入执行单次传递。 The savings can be significant when the iterator type is not a raw pointer, or even is just a model of the InputIterator concept (although in that case the interface would have to be changed, as the return type could not be copied, so one could eg return a value). 当迭代器类型不是原始指针时,节省可能很大,或者甚至只是InputIterator概念的模型(尽管在这种情况下接口必须被更改,因为无法复制返回类型,所以可以例如,返回一个值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM