简体   繁体   English

为什么lower_bound和upper_bound的谓词版本不一致地传递迭代器值?

[英]Why are the predicate version of lower_bound and upper_bound passing the iterator value inconsistently?

In upper_bound 's binary predicate, the iterator value is passed as the second argument while in lower_bound the iterator value is passed as the first argument. upper_bound的二进制谓词中,迭代器值作为第二个参数传递,而在lower_bound ,迭代器值作为第一个参数传递。

What is the reasoning here? 这是什么原因? And does it matter if I remember this detail or not when writing my own binary predicates? 如果我在编写自己的二元谓词时记住这个细节,那有关系吗?


NOTE my reference is www.cplusplus.com (which I am told may not be the best reference) and I confirmed it by looking at the implementation in the stl library shipped with VC++ . 注意我的参考是www.cplusplus.com(我被告知可能不是最好的参考),我通过查看VC++附带的stl库中的实现来确认它。

This is intentional. 这是故意的。 The reasoning is so you can use the same comparator for both algorithms. 原因是你可以为两种算法使用相同的比较器。 Consider the descriptions. 考虑一下说明。 lower_bound : lower_bound

Returns an iterator pointing to the first element in the range [first, last) that is not less than (ie greater or equal to) value. 返回指向范围[first,last]中不小于 (即大于或等于)值的第一个元素的迭代器。

and upper_bound : upper_bound

Returns an iterator pointing to the first element in the range [first, last) that is greater than value. 返回指向范围[first,last]中第一个元素的迭代器,该元素大于 value。

Consider that the standard comparator is < . 考虑标准比较器是< In order to implement both algorithms with only < would require !(elem < value) for one and value < elem for the other. 为了实现两个算法,只有< would require !(elem < value)为一个, value < elem为另一个。 The inversion of the order of arguments just follows directly from that. 论证顺序的倒置直接来自于此。

This shouldn't affect how you implement your Comparator though. 这不应该影响您实现Comparator的方式。

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

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