Skip to content
Related Articles

Related Articles

unordered_multiset insert() function in C++ STL
  • Difficulty Level : Hard
  • Last Updated : 09 Oct, 2018

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:






// 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;
}
Output:
ums contains: papaya pineapple mango cherry grapes banana apple orange

Program 2:




// 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;
}
Output:
ums contains: 8 7 9 3 2 4 6 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 :