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:
// 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; } |
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; } |
ums contains: 8 7 9 3 2 4 6 5
Recommended Posts:
- set insert() function in C++ STL
- deque insert() function in C++ STL
- multiset insert() function in C++ STL
- vector insert() function in C++ STL
- unordered_set insert() function in C++ STL
- map insert() in C++ STL
- list insert() in C++ STL
- emplace vs insert in C++ STL
- multimap insert() in C++ STL
- std::string::insert() in C++
- unordered_multimap insert() in C++ STL
- unordered_map insert in C++ STL
- 2-3 Trees | (Search and Insert)
- Insert string at specified position in PHP
- How to insert line break before an element using CSS?
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.