multimap insert() in C++ STL
The multimap::insert is a built-in function in C++ STL which is used to insert elements in the multimap container.
- Syntax:
iterator multimap_name.insert({key, element})Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the multimap container.
Return Value: The function returns an iterator pointing to the new element in the container.
// C++ program to illustrate// multimap::insert({key, element})#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermultimap<int,int> mp;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });mp.insert({ 3, 60 });mp.insert({ 2, 20 });mp.insert({ 5, 50 });// prints the elementscout <<"KEY\tELEMENT\n";for(autoitr = mp.begin(); itr != mp.end(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:KEY ELEMENT 1 40 2 30 2 20 3 60 5 50
- Syntax:
iterator multimap_name.insert(iterator position, {key, element})Parameters: The function accepts two parameters which is described below:
- {key, element}: this specifies a pair that consists of a key and element which is to be inserted into the multimap container.
- position: this does not specify the position where the insertion is to be done, it only points a position from where the searching operation for insertion is to be started. The insertion is done according to the order which is followed by the multimap container.
Return Value: The function returns an iterator pointing to the new element in the container.
// C++ program to illustrate// multimap::insert({key, element})#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermultimap<int,int> mp;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });autoit = mp.find(2);// inserts {3, 6} starting the search from// position where 2 is presentmp.insert(it, { 3, 60 });// prints the elementscout <<"KEY\tELEMENT\n";for(autoitr = mp.begin(); itr != mp.end(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:KEY ELEMENT 1 40 2 30 3 60
- Syntax:
iterator multimap_name.insert(iterator position1, iterator position2)
Parameters: The function accepts two parameters position1 and position2 which specifies the range of elements. All the elements in the range [position1, last) are inserted in the multimap container.
Return Value: The function returns an iterator pointing to the new element in the container.
// C++ program to illustrate// multimap::insert({key, element})#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermultimap<int,int> mp, mp1;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });// inserts all elements in range [begin, end)// in mp1mp1.insert(mp.begin(), mp.end());// prints the elementscout <<"Elements in mp1 are\n";cout <<"KEY\tELEMENT\n";for(autoitr = mp1.begin(); itr != mp1.end(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:Elements in mp1 are KEY ELEMENT 1 40 2 30
Recommended Posts:
- multimap::cbegin() and multimap::cend() in C++ STL
- multimap::crbegin() and multimap::crend() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- multimap equal_range() in C++ STL
- multimap operator = in C++ STL
- multimap rend in C++ STL
- multimap::operator= in C++ STL
- multimap::swap() in C++ STL
- multimap maxsize() in C++ STL
- multimap rbegin in C++ STL
- multimap::erase() in C++ STL
- multimap::count() in C++ STL
- multimap find() in C++ STL
- multimap key_comp in C++ STL
- multimap::emplace_hint() in C++ STL
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.

