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

Multiset Lower Bound Function

BLUF: Mastering Multiset Lower 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: Multiset Lower Bound Function

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

The lower_bound function in C++ for a multiset is employed to retrieve an iterator that points to the key in the multiset container that is equal to the specified val provided as an argument.

If a value is not found in the multiset container, it will return an iterator that points to the next element in the container which is greater than the specified value.

Syntax

Example

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

Parameter

val : The value that will be sought in the multiset container.

Return value

It yields an iterator that references the value within the multiset structure that matches the provided val parameter. If no such element exists, it returns end.

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data Races

Accessing the container does not result in any modifications to the multiset, whether it is the const version or the non-const version.

Simultaneously retrieving the elements of a container is considered to be a secure operation.

Exception Safety

If an error is raised, the multiset remains unchanged.

Example 1

Let's examine a straightforward example to retrieve the minimum bound of a specified key:

Example

#include <iostream>
#include <set>

using namespace std;

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

   cout << "Lower bound(=) of c is: " << *it;
   return 0;
}

Output:

Output

Lower bound(=) of c is: c

In the above example, lower bound of c is c.

Example 2

Let's explore a basic illustration of removing the element in a multiset from the lower bound to the upper bound:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<int> mymultiset;
  multiset<int>::iterator itlow,itup;

  for (int i=1; i<10; i++) mymultiset.insert(i*10); // 10 20 30 40 50 60 70 80 90

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

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

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

  return 0;
}

Output:

Output

mymultiset contains: 10 20 70 80 90

The erase method removes the elements in a multiset from the lower bound (inclusive) to the upper bound (exclusive) and then displays the remaining elements.

Example 3

Let's see a simple example:

Example

#include <set>  
#include <iostream>  

using namespace std;
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1;  
   multiset <int> :: const_iterator s1_AcIter, s1_RcIter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   s1_RcIter = s1.lower_bound( 20 );  
   cout << "The element of multiset s1 with a key of 20 is: "  
        << *s1_RcIter << "." << endl;  
  
   s1_RcIter = s1.lower_bound( 40 );  
  
   // If no match is found for the key, end( ) is returned  
   if ( s1_RcIter == s1.end( ) )  
      cout << "The multiset s1 doesn't have an element "  
           << "with a key of 40." << endl;  
   else  
      cout << "The element of multiset s1 with a key of 40 is: "  
           << *s1_RcIter << "." << endl;  
  
   // The element at a specific location in the multiset can be found   
   // by using a dereferenced iterator that addresses the location  
   s1_AcIter = s1.end( );  
   s1_AcIter--;  
   s1_RcIter = s1.lower_bound( *s1_AcIter );  
   cout << "The element of s1 with a key matching "  
        << "that of the last element is: "  
        << *s1_RcIter << "." << endl;  
        
        return 0;
}

Output:

Output

The element of multiset s1 with a key of 20 is: 20.
The multiset s1 doesn't have an element with a key of 40.
The element of s1 with a key matching that of the last element is: 30.

Example 4

Let's see a simple example:

Example

#include<set>
#include<iostream>

using namespace std;
 
int main()
{
 
    multiset<int> mp; 
    // insert elements in random order
    mp.insert( 2 );
    mp.insert( 1 );
    mp.insert( 5 );
    mp.insert( 4 );
    
    cout<<"Elements are: \n";
    for (auto it = mp.begin(); it != mp.end(); it++) {
        cout << (*it)<< endl;
    }
 
    // when 2 is present
    auto it = mp.lower_bound(2);
    cout << "The lower bound of key 2 is ";
    cout << (*it)<< endl;
 
    // when 3 is not present
    // points to next greater after 3
    it = mp.lower_bound(3);
    cout << "The lower bound of key 3 is ";
    cout << (*it)<< endl;
 
    // when 6 exceeds
    it = mp.lower_bound(6);
    cout << "The lower bound of key 6 is ";
    cout << (*it);
    
    return 0;
}

Output:

Output

Elements are: 
1
2
4
5
The lower bound of key 2 is 2
The lower bound of key 3 is 4
The lower bound of key 6 is 4

When attempting to determine the lower boundary of a value that surpasses the container, or in other words, a value not existing within the multiset container, the function will return an iterator pointing to the end position.

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