Set STL: lower_bound

In the given question, I wasn’t able to understand why 6 would be the answer. I tried it out and the compiler printed 6. In my understanding the set would be {1,2,5,7,10,12} and since lower bound returns the value of the first number that is >= the key, since no number that matches that for 15 is there I thought it’d return s.end()

Which is why I chose garbage, could you explain why it is 6.

Hey @singh_04
Return Value : The function returns an iterator pointing to the element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the parameter exceeds the maximum value in the container, then the iterator returned is equivalent to s.end() (A special iterator points beyond the last element).

And s.end() will return the number of elements int the set:

  1. https://ide.codingblocks.com/s/184917
  2. https://ide.codingblocks.com/s/184918

This is the reason it is returning 6.

Hope, this would help.
Give a like if you are satisfied.

1 Like