Lower bound in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand the lower bound in C++.

lower_bound() method in C++ is used to return the very first number in the container object which is not less than the given value.

Example

 Live Demo

#include <bits/stdc++.h>
int main(){
   std::vector<int> v{ 10, 20, 30, 40, 50 };
   std::cout << "Vector contains :";
   for (unsigned int i = 0; i < v.size(); i++)
      std::cout << " " << v[i];
   std::cout << "\n";
   std::vector <int>::iterator low1, low2;
   low1 = std::lower_bound(v.begin(), v.end(), 35);
   low2 = std::lower_bound(v.begin(), v.end(), 55);
   std::cout
      << "\nlower_bound for element 35 at position : "
      << (low1 - v.begin());
   std::cout
      << "\nlower_bound for element 55 at position : "
      << (low2 - v.begin());
   return 0;
}

Output

Vector contains : 10 20 30 40 50
lower_bound for element 35 at position : 3
lower_bound for element 55 at position : 5
raja
Published on 01-Apr-2020 10:10:09
Advertisements