简体   繁体   English

"stl中upper_bound和lower_bound的区别"

[英]Difference between upper_bound and lower_bound in stl

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 .其中l表示b的下限, u表示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.因此,如果相对于所使用的比较存在“相等”的值范围, lower_bound为您提供第一个, upper_bound为您提供这些的最后一个。 This is the normal pattern of STL ranges [first, last) .这是 STL 范围[first, last)的正常模式。

lower_bound : lower_bound

Returns an iterator pointing to the first element in the range [first,last) which does not compare less than val .返回指向范围 [first,last) 中第一个元素的迭代器,该元素的比较值不小于 val

upper_bound : upper_bound

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

Now there is a difference between being no less than something and greater than something.现在,不小于某物和大于某物是有区别的。

For example, if you compare 4 and 5 , you can say that例如,如果你比较45 ,你可以说

5 is _not less than_ 4
5 is _greater than_  4

However if you compare you compare 4 and 4 :但是,如果比较,则比较44

4 is _not less than_    4
4 is _not greater than_ 4

A simple answer is and less confusing WAY to remember this is below一个简单的答案是不那么令人困惑的记住这一点的方法如下

std::lower_bound - returns iterator to first element in the given range which is EQUAL_TO or Greater than val. std::lower_bound - 将迭代器返回到给定范围内的第一个元素,该元素是EQUAL_TO or Greater than val。

std::upper_bound - returns iterator to first element in the given range which is Greater than val . std::upper_bound - 将迭代器返回到给定范围内Greater than val第一个元素。

A simple answer from vscode来自 vscode 的简单回答

lower_bound: find the first pos in which val could be inserted without changing the order lower_bound:找到第一个可以插入 val 而不改变顺序的位置

upper_bound: find last postion in which val could be inserted without changing the order upper_bound:找到可以在不改变顺序的情况下插入 val 的最后一个位置

The simple answer is [ lower_bound, upper_bound )<\/strong>简单的答案是[ lower_bound, upper_bound )<\/strong>

s.lower_bound(t)<\/strong> will return iterator to the first element v in set such that v >= t s.lower_bound(t)<\/strong>将迭代器返回到集合中的第一个元素 v 使得 v >= t
s.upper_bound(t)<\/strong> will return iterator to the first element v in set such that v > t. s.upper_bound(t)<\/strong>将迭代器返回到集合中的第一个元素 v,使得 v > t。

When we often call xxxxx_bound for the STL set or map,当我们经常调用 xxxxx_bound 为 STL set 或 map 时,
we often want to find the data in some range.我们经常想找到某个范围内的数据。

I share some usages of lower_bound & upper_bound samples here.我在这里分享一些lower_bound 和upper_bound 样本的用法。 So it could be easy for everyone to use it and remember it.因此,每个人都可以轻松使用并记住它。

iterate all values in [A, B)迭代 [A, B) 中的所有值<\/h2>
set<int> s = {0,1,2,10,11,12,15}; int A=1, B=11; for(auto iter = s.lower_bound(A); iter != s.lower_bound(B); iter++) { cout<<*iter<<"\\t"; }<\/code><\/pre>

Result结果

1 2 10<\/code><\/pre>

It show all v in set s satsify 1 < v <= 11<\/strong> aka all v in [1, 11)<\/strong>它显示集合 s 中的所有 v 满足1 < v <= 11<\/strong>也就是[1, 11)<\/strong>中的所有 v

iterate all values in [A, B]迭代 [A, B] 中的所有值<\/h2>
set<int> s = {0,1,2,10,11,12,15}; int A=1, B=11; for(auto iter = s.lower_bound(A); iter != s.upper_bound(B); iter++) { cout<<*iter<<"\\t"; }<\/code><\/pre>

Result结果

1 2 10 11<\/code><\/pre>

It show all v in set s satsify 1 <= v <= 11<\/strong> aka all v in [1, 11]<\/strong>它显示集合 s 中的所有 v 满足1 <= v <= 11<\/strong>也就是[1, 11]<\/strong>中的所有 v

iterate all values in (A, B)迭代 (A, B) 中的所有值<\/h2>
set<int> s = {0,1,2,10,11,12,15}; int A=1, B=11; for(auto iter = s.upper_bound(A); iter != s.lower_bound(B); iter++) { cout<<*iter<<"\\t"; }<\/code><\/pre>

Result结果

2 10<\/code><\/pre>

It show all v in set s satsify 1 < v < 11<\/strong> aka all v in (1, 11)<\/strong>它显示集合 s 中的所有 v 满足1 < v < 11<\/strong>也就是(1, 11)<\/strong>中的所有 v

Iterate all values in (A, B]迭代 (A, B] 中的所有值<\/h2>
set<int> s = {0,1,2,10,11,12,15}; int A=1, B=11; for(auto iter = s.upper_bound(A); iter != s.upper_bound(B); iter++) { cout<<*iter<<"\\t"; }<\/code><\/pre>

Result结果

2 10 11<\/code><\/pre>

It show all v in set s satsify 1 < v <= 11<\/strong> aka all v in (1, 11]<\/strong>它显示集合 s 中的所有 v 满足1 < v <= 11<\/strong> aka all v in (1, 11]<\/strong>

"

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

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