CantLoseNow's blog

By CantLoseNow, history, 18 months ago, In English

see this imageand then this image . why they are giving different outputs when i want the lower bound of 6?

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

lower_bound or any other binary search function can only apply on sorted containers, so in the case of the first picture, try sorting the vector before calling lower_bound. in the second picture, you are calling lower_bound on a reverse sorted container (sorted from greater to smaller) which i think is going to find the first element which is smaller or equal to provided integer starting from the greatest element. you can think of it this way: usually, we store elements in the multiset or set as multiplied with -1 instead of using greater<>, then the set is going to be {-8, -7, -5, -5, -3} now instead of lower_bound(6) we call lower_bound(-6) which in this case is going to be -5 which is the first element greater or equal to -6, so the answer is -(-5) = 5.

now whenever you found yourself asking about the behavior of some class or function in c++, you can search for the name of the class on google and find some documentation, usually, they are more than enough, and it's even better to find some good documentation of standard containers and algorithm and study it all, as it will save a lot of try and error and maybe some wrong verdicts in contests.

good luck!

  • »
    »
    18 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thanks a lot...but one thing i wanted to say..the image 1 has a sorted vector in non-increasing order. doesn't it count as sorted?

    • »
      »
      »
      18 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      no, you can try lower_bound(vec.rbegin(), vec.rend()).