upper_bound in C++

C++Server Side ProgrammingProgramming

Here we will see that is the upper_bound() function in C++ STL. This function returns an iterator that points to the first element in the container, which is considered to go after val. The syntax is like:

iterator upper_bound (const value_type& val);
const_iterator upper_bound (const value_type& val) const;

The return value is an iterator, pointing to the first element in the container which is considered to go after val.

Example

 Live Demo

#include <iostream>
#include <set>
using namespace std;
int main () {
   set<int> myset;
   set<int>::iterator itlow,itup;
   for (int i = 1; i < 10; i++) myset.insert(i*10);
   itup = myset.upper_bound (60);
   myset.erase(itup);
   cout << "myset contains:";
   for (set<int>::iterator it = myset.begin(); it!=myset.end(); ++it)
   cout << ' ' << *it;
}

Output

myset contains: 10 20 30 40 50 60 80 90
raja
Published on 30-Dec-2019 10:02:52
Advertisements