简体   繁体   English

对的 Map 中的 lower_bound 和 upper_bound

[英]lower_bound and upper_bound in Map of Pairs

I am trying to understand the lower_bound and upper_bound in map of pairs from here .我试图从这里了解 map 对中的lower_boundupper_bound

The definitions are like below:定义如下:

lower_bound: In map of pairs lower_bound() for pair(x, y) will return an iterator pointing to the pair whose first value is greater than or equals x and second value is greater than or equals to y. lower_bound:在 map 中, pair(x, y)lower_bound( ) 将返回一个迭代器,该迭代器指向第一个值大于或等于 x 且第二个值大于或等于 y 的对。 If the above-mentioned criteria are not met, then it returns an iterator which points to the pair {map.size(), 0} .如果不满足上述条件,则返回一个指向对{map.size(), 0}的迭代器。

upper_bound: In map of pairs upper_bound() for pair(x, y) will return an iterator pointing to the pair whose first value is greater than x and second value is greater than y. upper_bound:在 map 中, pair(x, y)upper_bound( ) 将返回一个迭代器,该迭代器指向第一个值大于 x 且第二个值大于 y 的对。 If the above-mentioned criteria are not met, then it returns an iterator which points to the pair {map.size(), 0} .如果不满足上述条件,则返回一个指向对{map.size(), 0}的迭代器。

Now, let's consider the below example.现在,让我们考虑下面的例子。

map<pair<int, int>, int> mp;

mp.insert({ { 2, 3 }, 8 });
mp.insert({ { 2, 5 }, 5 });
mp.insert({ { 7, 1 }, 3 });
mp.insert({ { 9, 3 }, 1 });
mp.insert({ { 5, 0 }, 3 });

We have above map of pairs.我们有上面的 map 对。

Now, if I want to find the lower bound of pair {2, 4} , the result is {2, 5} which seems fine to me according to the definition.现在,如果我想找到对{2, 4}的下限,结果是 {2, 5} 根据定义对我来说似乎很好。 The definition says that " lower_bound will return an iterator pointing to the pair whose first value is greater than or equals x and second value is greater than or equals to y ".定义说“ lower_bound 将返回一个迭代器,该迭代器指向第一个值大于或等于 x 且第二个值大于或等于 y 的对”。 So, in this case 2 is equal to 2 and 5 is greater than 4.因此,在这种情况下,2 等于 2,5 大于 4。

But, if I want to find the upper bound of pair {2,2} , the result is {2, 3} which seems to me wrong according to the definition.但是,如果我想找到对{2,2}的上限,结果是{2, 3}根据定义在我看来这是错误的。 The definition says that " upper_bound will return an iterator pointing to the pair whose first value is greater than x and second value is greater than y " But in the above case, first value is equal to x.定义说“ upper_bound 将返回一个迭代器,该迭代器指向第一个值大于 x 且第二个值大于 y的对”但在上述情况下,第一个值等于 x。 According to the definition, the upper bound of {2,2} should be {9, 3} where 9 is greater than 2 and 3 is greater than 2.根据定义, {2,2}的上界应为{9, 3} ,其中 9 大于 2,3 大于 2。

I think I missed something here.我想我在这里错过了一些东西。 Can anyone please help me out here?任何人都可以在这里帮助我吗?

You are storing this by key value in an ordered map.您通过键值将其存储在有序的 map 中。 The ordering of the maps' keys will be as follows:地图键的顺序如下:

<2, 3> | <2, 5> | <5, 0> | <7, 1> | <9, 3> 

See here.看这里。

So, the actual std::map::upper_bound reference says the following:因此,实际的std::map::upper_bound参考说明如下:

iterator upper_bound( const Key& key );

Returns an iterator pointing to the first element that is greater than key.返回一个迭代器,指向大于 key 的第一个元素。

From this it is easy to see, the first key in the list that is greater than <2, 2> is <2, 3> (for the call mp.upper_bound(std::make_pair(2, 2)) ).由此不难看出,列表中大于<2, 2>的第一个键是<2, 3> (对于调用mp.upper_bound(std::make_pair(2, 2)) )。

For upper_bound greater means greater according to the type of the key in the map.根据 map 中的密钥类型,对于 upper_bound greater意味着更大。 The key in this case is a pair of ints and the operator< for a pair of ints is used for this comparison.在这种情况下,键是一对整数,并且一对整数的operator<用于此比较。 According to the definition of this operator if the first element of the pair is the same, the second elements are compared, so indeed {2, 3} is greater than {2, 2} .根据该运算符的定义,如果对的第一个元素相同,则比较第二个元素,因此确实{2, 3}大于{2, 2}

Here is link to the gnu implementation for reference.这是gnu 实现的链接以供参考。

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

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