unordered_multiset insert() function in C++ STL
Last Updated :
24 Jun, 2021
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
#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);
ums.insert(arr.begin(), arr.end());
ums.insert({ "pineapple" , "papaya" });
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
#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);
ums.insert(x.begin(), x.end());
ums.insert({ 7, 8 });
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
Share your thoughts in the comments
Please Login to comment...