在 upper_bound
的二进制谓词中,迭代器值作为第二个参数传递,而在 lower_bound
中,迭代器值作为第一个参数传递。
这里的推理是什么?在编写自己的二元谓词时,我是否记得这个细节有关系吗?
注意 我的引用是 www.cplusplus.com(有人告诉我这可能不是最好的引用)并且我通过查看 STL
中的实现来确认它VC++
附带的库。
最佳答案
这是故意的。原因是您可以对两种算法使用相同的比较器。考虑描述。 lower_bound
:
Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value.
和 upper_bound
:
Returns an iterator pointing to the first element in the range [first, last) that is greater than value.
考虑标准比较器是 <
.为了仅使用 <
来实现这两种算法需要 !(elem < value)
对于一个和value < elem
对于另一个。参数顺序的倒置直接由此而来。
不过,这不应影响您实现 Comparator 的方式。
关于c++ - 为什么 lower_bound 和 upper_bound 的谓词版本不一致地传递迭代器值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485437/