问题描述
在这些页面上,我一直在研究stl中的upper_bound和lower_bound算法的工作方式: lower_bound , upper_bound ,并且在这些页面上的记录方式也相同: lower_bound , upper_bound
从链接中查看代码,他们似乎对我做的完全相同,只是以下几行有所不同(从前两个链接中查看代码):
lower_bound(第10行):
if(* it< val){//或:if(comp(* it,val)),对于版本(2)
上界(第10行):
if(!(val< * it))////或:if(!comp(val,* it)),对于版本(2)
但是一定要反转比较的元素,然后将它们与false进行比较是双重否定的,因此它们做的完全相同吗?
我实际上没有看到区别吗,这是网站文档中的错误吗?如果是后者,正确的方法是什么?
值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之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!