multimap::upper_bound() method in C++ STL

Multimaps are associative containers available in the C++ Standard Library. They are similar to a map with an addition that multiple elements can have the same keys. The multimap::upper_bound() is a method available in the STL that returns an iterator pointing to the first element in the container whose key is considered to go after the specified key.

Syntax:

map_name.upper_bound( val );

Parameters: The multimap::upper_bound() accepts a single parameter:
val: Key to search for
Return value: An iterator to the first element whose key goes after val. multimap::end() otherwise

Example of multimap::upper_bound() method

#include<iostream>
#include<map>

using namespace std;


int main()
{
    multimap<char,int> m;

    m.insert( pair<int,int> ('c',1) );
    m.insert( pair<int,int> ('b',2) );
    m.insert( pair<int,int> ('a',3) );
    m.insert( pair<int,int> ('d',9) );
    m.insert( pair<int,int> ('d',7) );
    m.insert( pair<int,int> ('d',4) );
    m.insert( pair<int,int> ('e',10) );

    cout<< "upper bound value of key 'd' is: \n";
    cout<< m.upper_bound('d')->first <<" : "<< m.upper_bound('d')->second;
}

Output:

upper bound value of key 'd' is:
e : 10

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top