unordered_multimap insert() in C++ STL

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 paramters:

  • 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

A: 1
B: 2
C: 3
D: 4
E: 5

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

G: 6
A: 1
b: 2
c: 3
d: 4
e: 5

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

G: 7
F: 6
A: 1
B: 2
C: 3
D: 4
E: 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.