Skip to content
Related Articles

Related Articles

unordered_multiset insert() function in C++ STL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 24 Jun, 2021
View Discussion
Improve Article
Save Article

The unordered_multiset::insert() is a built-in function in C++ STL that inserts new elements in the unordered_multiset. This increases the container size. Also notice that elements with the same value are also stored as many times they are inserted. 
Syntax: 
 

Unordered_multiset_name.insert(element)

Parameters: This function accepts a single parameter element. It specifies the element which is to be inserted into the container.
Return value: The function returns an iterator to the newly inserted element.
Below programs illustrate the above function: 
Program 1: 
 

CPP




// 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, 3> arr = { "cherry", "mango", "apple"};
    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;
}


Output

ums contains: papaya pineapple mango cherry grapes banana apple apple orange

Program 2: 
 

CPP




// 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);
      x.push_back(4);
    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;
}


Output

ums contains: 8 7 9 3 2 4 4 6 5

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!