Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ set::upper_bound function is used to return an iterator pointing to the first element in the set container which is considered to go after the specified value. If all elements of the set are considered to go before the specified value, then the iterator points to set::end.

Syntax

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

Parameters

val Specify value to compare.

Return Value

An iterator pointing to the first element in the set container which is considered to go after the specified value, or set::end if all elements of the set is considered to go before the specified value.

Time Complexity

Logarithmic i.e, Θ(log(n))

Example:

In the below example, the set::lower_bound function is used with set::upper_bound to specify the lower and upper bound in the set called MySet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> MySet{10, 20, 30, 40, 50, 60, 70, 80};
  set<int>::iterator it, itlower, itupper;

  itlower = MySet.lower_bound(25);
  itupper = MySet.upper_bound(50);
  MySet.erase(itlower, itupper);

  cout<<"MySet contains: ";
  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

MySet contains:  10 20 60 70 80

❮ C++ - Set