stl中upper_bound和lower_bound之间的区别 [英] Difference between upper_bound and lower_bound in stl

查看:54
本文介绍了stl中upper_bound和lower_bound之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这些页面上,我一直在研究stl中的upper_bound和lower_bound算法的工作方式: lower_bound upper_bound ,并且在这些页面上的记录方式也相同: lower_bound ,解决方案

 值a a b b b c c c索引0 1 2 3 4 5 6 7 8束缚你 

其中 l 表示 b 的下限,而 u 表示 b 的上限./p>

因此,如果相对于正在使用的比较而言,值的范围是相等",则 lower_bound 为您提供第一个,而 upper_bound 为您提供-这些的过去.这是STL范围的正常模式 [first,last).

I was looking at how the upper_bound and lower_bound algorithms work in stl on these pages: lower_bound, upper_bound, and it's documented the same way on these pages: lower_bound, upper_bound

Looking at the code from the links, they seem to do exactly the same thing to me, with only the following lines being different (looking at the code from the first 2 links):

lower_bound (line 10):

if (*it<val) {                 // or: if (comp(*it,val)), for version (2)

upper_bound (line 10):

 if (!(val<*it))                // or: if (!comp(val,*it)), for version (2) 

but surely reversing the compared elements and then comparing them to false is a double negative, and thus they do exactly the same thing?

Is there actually a difference that I'm just not seeing, Is this an error in the documentation on the websites? If the latter, what would be the correct way?

解决方案

value a a a b b b c c c
index 0 1 2 3 4 5 6 7 8
bound       l     u

Where l represents the lower bound of b, and u represents the upper bound of b.

So if there are range of values that are "equal" with respect to the comparison being used, lower_bound gives you the first of this, upper_bound gives you one-past-the-end of these. This is the normal pattern of STL ranges [first, last).

这篇关于stl中upper_bound和lower_bound之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆