multiset lower_bound() in C++ STL with Examples

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand multiset lower_bound() in C++ STL.

The function lower_bound() returns the first existence of the element in the container equivalent to the provided parameter, else it returns the element immediately bigger than that.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   multiset<int> s;
   s.insert(1);
   s.insert(2);
   s.insert(2);
   s.insert(1);
   s.insert(4);
   cout << "The multiset elements are: ";
   for (auto it = s.begin(); it != s.end(); it++)
      cout << *it << " ";
   auto it = s.lower_bound(2);
   cout << "\nThe lower bound of key 2 is ";
   cout << (*it) << endl;
   it = s.lower_bound(3);
   cout << "The lower bound of key 3 is ";
   cout << (*it) << endl;
   it = s.lower_bound(7);
   cout << "The lower bound of key 7 is ";
   cout << (*it) << endl;
   return 0;
}

Output

The multiset elements are: 1 1 2 2 4
The lower bound of key 2 is 2
The lower bound of key 3 is 4
The lower bound of key 7 is 5
raja
Published on 06-Apr-2020 14:01:48
Advertisements