Lower_bound for sets

sets;
s.insert(1);
s.insert(5);
s.insert(7);
s.insert(2);
s.insert(12);
s.insert(10);
for(auto i:s)
cout<<i<<" ";
cout<<endl;
auto it = s.lower_bound(15);
cout<< *it <<endl;

As the lower_bound returns the pointer equals to or just greater than that element, if the element doesn’t found it returns the end of the set, but it here answer is 6.Can you explain why?

If all the elements in the range compare less than val, the lower_bound function returns last

here set consists of 6 elements indexing from 0 to 5 hence last is 6

Do “it” points to the end of the set?
If it points to the end then the answer might be any garbage value right.
it-s.begin() gives us index …right then why *it is giving the index?
Can you clearly explain?

Yes

but here it just return last
you can say it’s index not iterator
this is how lower_bound works , no logic for this when it is built in library then it is defined as such

Thank you for the reply🙂

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

@sivaganeshdaggubati kindly give your feedback it hardly takes a min

hi @sivaganeshdaggubati
u have reopened the doubt… is there still anything which is not clear??

Yaa i am still unable to understand how the *it gives index :(.Can you explain?

ig @asaurabh_26
has already explained in detail above… i don’t think there is anything else I can add to it… rest if u want to learn more about inbuilt functions, u can google it out and go through different examples…

Ok Thank you for the reply

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.