C++ Standard Library C++ STL Library

C++ multiset - upper_bound() Function



The C++ multiset::upper_bound function returns an iterator pointing to the first element in the multiset container which is considered to go after the specified value. If all elements of the multiset are considered to go before the specified value, then the iterator points to multiset::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 multiset container which is considered to go after the specified value, or multiset::end if all elements of the multiset is considered to go before the specified value.

Time Complexity

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

Example:

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

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

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

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

  return 0;
}

The output of the above code will be:

MyMSet contains:  10 20 60 70 80

❮ C++ <set> Library