Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

Working of lower_bound and upper_bound functions in set < pair < int , int > >

Revision en2, by sidforces, 2020-04-09 16:07:32

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 but the confusion still remains. Like I write this code-

set<pii> ms;

for(int i=0;i<10;i=i+2)





int 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.

Tags #set, lower_bound, upper_bound, pair


  Rev. Lang. By When Δ Comment
en2 English sidforces 2020-04-09 16:07:32 100
en1 English sidforces 2020-04-09 16:04:08 1463 Initial revision (published)