Home c++ What is upper_bound and lower_bound in C++ and how are they different

What is upper_bound and lower_bound in C++ and how are they different

Author

Date

Category

Please tell me what upper_bound and lower_bound are in the C++ Standard Library and how they work.


Answer 1, authority 100%

Well, for example, you have a ordered (important!) sequence

1 2 3 4 4 4 5 5 5 5 5 6 7

When looking for upper_bound for a value of 5 this there will be an iterator (a pointer if the concept of an iterator is new to you) to the value 6 – the first larger value .

Accordingly, lower_bound will point to the first not less value – the first 5.

For example code

int main (int argc, const char * argv [])
{
  int a [20] = {1,2,3,4,5,6,7,7,7,7,8,9,9,11,11,12,16,20,20,20,20};
  for (int * p = lower_bound (a, a + 20,10);
    p! = upper_bound (a, a + 20.20); ++ p)
    cout & lt; & lt; * p & lt; & lt; "";
  cout & lt; & lt; endl;
}

gives

11 11 12 16 20 20 20 20

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions