Can anyone explains me how the lower_bound and upper_bound functions work in set<pair<int,int> >. I mean to ask how they actually match any pair in the set with the given parameters step by step. Like i read somewhere that it first checks the first int and then goes for second so, what it actually does? I also refered this blog https://codeforces.com/blog/entry/17077 but the confusion still remains. Like I write this code-

set<pii> ms; for(int i=0;i<10;i=i+2) ms.insert(mp(i,i+1)); ms.insert(mp(2,4)); ms.insert(mp(2,2)); ms.insert(mp(4,4)); int x,y; cin>>x>>y; cout<<ms.lower_bound(mp(x,y))->first<<" "<<ms.lower_bound(mp(x,y))->second<<endl; cout<<ms.upper_bound(mp(x,y))->first<<" "<<ms.upper_bound(mp(x,y))->second<<endl;

and on the input of (2,2) it is giving the output (2,2) for lowerbound and (2,3) for upperbound then why it is giving the output for upperbound as (2,3) because upperbound gives the just > value and it checks first int first then all pairs with '2' as first element should be discarded.And on the input of (7,10) output is (8,9) for both the functions. Now it is just checking the first int and ignoring the second int '10' because upperbound of 10 cannot be 9.

So, please if anybody can clear my confusions then please comment and may be I am understanding it totally wrong and asking the wrong question so please inlight me where i am wrong.

Thanks in advance.

Auto comment: topic has been updated by sidforces (previous revision, new revision, compare).I don't really understand your question so forgive me if this answers a different thing.

First let me explain how the pairs are sorted

Pairs are sorted first based on the leftmost values then by their rightmost values

So if their leftmost values are equal they are sorted based on their rightmost values

Let me give two examples $$$(-1, 2)$$$ is less than $$$(3, 2)$$$ and $$$(2, 2)$$$ is less than $$$(2, 3)$$$

So your set is sorted like this

$$$(0, 1)$$$ $$$<$$$ $$$(2, 2)$$$ $$$<$$$ $$$(2, 3)$$$ $$$<$$$ $$$(2, 4)$$$ $$$<$$$ $$$(4, 4)$$$ $$$<$$$ $$$(4, 5)$$$ $$$<$$$ $$$(6, 7)$$$ $$$<$$$ $$$(8, 9)$$$

LOWER BOUNDis the first element greater than or equal to the given inputUPPER BOUNDis the first element strictly greater than the given inputSo from the definition of

LOWER BOUNDit should now be obvious that theLOWER BOUNDfor $$$(2, 2)$$$ is $$$(2, 2)$$$ since it's the first element equal to the given input and theUPPER BOUNDis $$$(2, 3)$$$ since it's the first element strictly greater than $$$(2, 2)$$$.Same goes for $$$(7, 10)$$$.

Hope this helps)

thanks a lot. Your concept of considering the element in the set and then think about the upper_bound is very nice. This removes my confusion.

When no comparator function is specified while constructing the set, set invokes

`operator<`

for comparing elements.And from cppreference

That's why because of the lexicographic order`operator<`

for pair`(2, 2)`

is lesser than`(2, 3)`

and`(7, 10)`

is lesser than`(8, 9)`

.