unordered_multimap insert() in C++ STL
The function std::unordered_multimap::insert() is a built-in function in C++ STL that extends container by inserting new element in unordered_multimap. This function increases container size by one. The insert() function can be used to insert a single key-value pair, a complete unordered_map, initialized list insertion etc.
Syntax:
iterator insert(const_iterator position, const value_type& val);
Parameters: This method takes following paramters:
- Position: position to insert element
- Value: value to be inserted
Return Value: This method returns an iterator pointing to the newly inserted element.
Time Complexity:
- O(1) in average case
- O(n) in worst case
Below programs illustrate the unordered_multimap::insert function:
Program 1:
#include <iostream> #include <unordered_map> using namespace std; int main(void) { // Declare unordered_multimap unordered_multimap<char, int> cmap = { { 'B', 2 }, { 'C', 3 }, { 'D', 4 }, { 'E', 5 }, }; // insert a key-value pair using insert() auto pos = cmap.insert(cmap.begin(), pair<char, int>('A', 1)); // print the map with the newly inserted key-value pair for (auto x : cmap) cout << x.first << ": " << x.second << endl; return 0; } |
A: 1 B: 2 C: 3 D: 4 E: 5
Program 2:
#include <iostream> #include <unordered_map> using namespace std; int main() { // Declare unordered_multimap unordered_multimap<char, int> cmap = { { 'b', 2 }, { 'c', 3 }, { 'd', 4 }, { 'e', 5 }, }; unordered_multimap<char, int> dmap = { { 'A', 1 }, { 'G', 6 } }; // Insert the map from begin to end cmap.insert(dmap.begin(), dmap.end()); // Print the map for (auto x : cmap) cout << x.first << ": " << x.second << endl; return 0; } |
G: 6 A: 1 b: 2 c: 3 d: 4 e: 5
Program 3:
#include <iostream> #include <unordered_map> using namespace std; int main() { // Declare unordered_multimap unordered_multimap<char, int> cmap = { { 'B', 2 }, { 'C', 3 }, { 'D', 4 }, { 'E', 5 }, }; // Insert a new list cmap.insert({ { 'A', 1 }, { 'F', 6 }, { 'G', 7 } }); // Print the map for (auto x : cmap) cout << x.first << ": " << x.second << endl; return 0; } |
G: 7 F: 6 A: 1 B: 2 C: 3 D: 4 E: 5
Recommended Posts:
- map insert() in C++ STL
- multimap insert() in C++ STL
- unordered_map insert in C++ STL
- set insert() function in C++ STL
- std::string::insert() in C++
- emplace vs insert in C++ STL
- list insert() in C++ STL
- 2-3 Trees | (Search and Insert)
- Insert string at specified position in PHP
- unordered_set insert() function in C++ STL
- unordered_multiset insert() function in C++ STL
- deque insert() function in C++ STL
- multiset insert() function in C++ STL
- vector insert() function in C++ STL
- 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.



