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 << "
";    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       << "
lower_bound for element 35 at position : "       << (low1 - v.begin());    std::cout       << "
lower_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
Updated on 01-Apr-2020 06:41:03

Advertisements