unordered_multiset insert() function in C++ STL

The unordered_multiset::insert() is a built-in function in C++ STL which inserts new elements in the unordered_multiset. Thus increases the container size.

Syntax:

Unordered_multiset_name.insert(element)

Parameters: This function accepts a single parameter element. It specifies the element which is to be inserted in the container.

Return value: The function returns an iterator to the newly inserted element.

Below programs illustrate the above function:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// unordered_multiset::insert
#include <array>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
  
int main()
{
    unordered_multiset<string> ums = { "apple", "orange", "banana" };
    array<string, 2> arr = { "cherry", "mango" };
    string str = "grapes";
  
    ums.insert(str); // copy insertion
    ums.insert(arr.begin(), arr.end()); // range insertion
    ums.insert({ "pineapple", "papaya" }); // initializer list insertion
  
    cout << "ums contains:";
    for (const string& x : ums)
        cout << " " << x;
    cout << endl;
  
    return 0;
}

chevron_right


Output:

ums contains: papaya pineapple mango cherry grapes banana apple orange

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// unordered_multiset::insert
#include <array>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    unordered_multiset<int> ums = {2, 4, 6};
    vector<int> x;
    x.push_back(3);
    x.push_back(9);
    int val = 5;
     
    ums.insert(val); // copy insertion
    ums.insert(x.begin(), x.end()); // range insertion
    ums.insert({ 7, 8 }); // initializer list insertion
  
    cout << "ums contains:";
    for (const int& x : ums)
        cout << " " << x;
    cout << endl;
  
    return 0;
}

chevron_right


Output:

ums contains: 8 7 9 3 2 4 6 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.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.