Set Upper Bound Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Upper Bound Function

Set Upper Bound Function

BLUF: Mastering Set Upper Bound Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Upper Bound Function

C++ is renowned for its efficiency. Learn how Set Upper Bound Function enables low-level control and high-performance computing in the tutorial below.

C++ set upper_bound

The C++ set upper_bound method is employed to retrieve an iterator indicating the element in the set container that is greater than the specified value, val.

Syntax

Example

iterator upper_bound (const value_type& val) const;            //until C++ 11

      iterator upper_bound (const value_type& val);                    //since C++ 11
const_iterator upper_bound (const value_type& val) const;        //since C++ 11

Parameter

val : value to be searched in the set container.

Return value

It yields an iterator that references the value in the set container exceeding the val provided as an argument. If no such element exists, it will return end.

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data Races

The container is accessed without any modifications made to it by either the const or non-const versions.

Simultaneously retrieving the elements of a set is considered secure.

Exception

If an error occurs and an exception is thrown, the container remains unchanged.

Example 1

Let's examine a straightforward example to determine the maximum limit of a specified value:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<char> m = {'a', 'b', 'c', 'd'};
           
   auto it = m.upper_bound('b');

   cout << "Upper bound of b is(>): " << *it << endl;

   return 0;
}

Output:

Output

Upper bound of b is(>): c

When attempting to determine the upper limit of the element b as shown above, the result will be the element c, which is larger than b.

Example 2

Let's examine a basic illustration of removing elements from a set starting from the lower bound up to the upper bound:

Example

#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); // 10 20 30 40 50 60 70 80 90

  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //                   ^

  myset.erase(itlow,itup);                     // 10 20 70 80 90

  std::cout << "myset contains:";
  for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

Output:

Output

myset contains: 10 20 70 80 90

The erase method removes the elements of a set starting from the lower bound (inclusive) up to the upper bound (exclusive) and then displays the remaining content.

Example 3

Let's see a simple example:

Example

#include<iostream>
#include<set>
using namespace std;
 
int main()
{
    // initialize container
    set<int> mp;
 
    // insert elements in random order
    mp.insert( 12 );
    mp.insert( 11 );
    mp.insert( 15 );
    mp.insert( 14 );
 
    // when 11 is present
    auto it = mp.upper_bound(11);
    cout << "The upper bound of key 11 is ";
    cout << (*it)<< endl;
 
    // when 13 is not present
    it = mp.upper_bound(13);
    cout << "The upper bound of key 13 is ";
    cout << (*it)<< endl;
 
    // when 17 is exceeds the maximum key, so size
        // of mp is returned as key and value as 0.
    it = mp.upper_bound(17);
    cout << "The upper bound of key 17 is ";
    cout << (*it);
    return 0;
}

Output:

Output

The upper bound of key 11 is 12
The upper bound of key 13 is 14
The upper bound of key 17 is 4

When seeking the upper limit of a value that is absent in the set container but does not surpass the maximum value, it will yield the next greater value. For instance, requesting the upper bound of 13 will result in 14. Conversely, if attempting to find the upper bound of a value beyond both the set and the container's maximum value, it will return end.

Example 4

Let's see a simple example:

Example

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;     
   set <int> s1;  
   set <int> :: const_iterator s1_AcIter, s1_RcIter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   s1_RcIter = s1.upper_bound( 20 );  
   cout << "The first element of set s1 with a key greater "  
        << "than 20 is: " << *s1_RcIter << "." << endl;  
  
   s1_RcIter = s1.upper_bound( 30 );  
  
   // If no match is found for the key, end( ) is returned  
   if ( s1_RcIter == s1.end( ) )  
      cout << "The set s1 doesn't have an element "  
           << "with a key greater than 30." << endl;  
   else  
      cout << "The element of set s1 with a key > 40 is: "  
           << *s1_RcIter << "." << endl;  
  
   // The element at a specific location in the set can be found   
   // by using a dereferenced iterator addressing the location  
   s1_AcIter = s1.begin( );  
   s1_RcIter = s1.upper_bound( *s1_AcIter );  
   cout << "The first element of s1 with a key greater than"  
        << endl << "that of the initial element of s1 is: "  
        << *s1_RcIter << "." << endl;  
        
        return 0;
}

Output:

Output

The first element of set s1 with a key greater than 20 is: 30.
The set s1 doesn't have an element with a key greater than 30.
The first element of s1 with a key greater than
that of the initial element of s1 is: 20.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience