Skip to content
Related Articles

Related Articles

Improve Article

unordered_multimap insert() in C++ STL

  • Difficulty Level : Medium
  • Last Updated : 29 Jul, 2021

The function std::unordered_multimap::insert() is a built-in function in C++ STL that extends container by inserting new element in unordered_multimap. This function increases container size by one. The insert() function can be used to insert a single key-value pair, a complete unordered_map, initialized list insertion etc.
 

Syntax:  

iterator insert(const_iterator position, const value_type& val);

Parameters: This method takes following parameters: 

  • Position: position to insert element
  • Value: value to be inserted

Return Value: This method returns an iterator pointing to the newly inserted element.
Time Complexity: 

  • O(1) in average case
  • O(n) in worst case

Below programs illustrate the unordered_multimap::insert function:
Program 1: 



CPP




#include <iostream>
#include <unordered_map>
 
using namespace std;
int main(void)
{
    // Declare unordered_multimap
    unordered_multimap<char, int> cmap = {
        { 'B', 2 },
        { 'C', 3 },
        { 'D', 4 },
        { 'E', 5 },
    };
 
    // insert a key-value pair using insert()
    auto pos
        = cmap.insert(cmap.begin(),
                      pair<char, int>('A', 1));
 
    // print the map with the newly inserted key-value pair
    for (auto x : cmap)
        cout << x.first << ": "
             << x.second << endl;
 
    return 0;
}
Output: 
A: 1
B: 2
C: 3
D: 4
E: 5

 

Program 2: 

CPP




#include <iostream>
#include <unordered_map>
 
using namespace std;
int main()
{
    // Declare unordered_multimap
    unordered_multimap<char, int> cmap = {
        { 'b', 2 },
        { 'c', 3 },
        { 'd', 4 },
        { 'e', 5 },
    };
 
    unordered_multimap<char, int> dmap
        = { { 'A', 1 },
            { 'G', 6 } };
 
    // Insert the map from begin to end
    cmap.insert(dmap.begin(), dmap.end());
 
    // Print the map
    for (auto x : cmap)
        cout << x.first << ": "
             << x.second << endl;
 
    return 0;
}
Output: 
G: 6
A: 1
b: 2
c: 3
d: 4
e: 5

 

Program 3: 

CPP




#include <iostream>
#include <unordered_map>
 
using namespace std;
int main()
{
    // Declare unordered_multimap
    unordered_multimap<char, int> cmap = {
        { 'B', 2 },
        { 'C', 3 },
        { 'D', 4 },
        { 'E', 5 },
    };
 
    // Insert a new list
    cmap.insert({ { 'A', 1 },
                  { 'F', 6 },
                  { 'G', 7 } });
 
    // Print the map
    for (auto x : cmap)
        cout << x.first << ": "
             << x.second << endl;
 
    return 0;
}
Output: 
G: 7
F: 6
A: 1
B: 2
C: 3
D: 4
E: 5

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :