The multimap::insert is a built-in function in C++ STL that 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.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
multimap< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 2, 20 });
mp.insert({ 5, 50 });
cout << "KEY\tELEMENT\n" ;
for ( auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first << '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output
KEY ELEMENT
1 40
2 30
2 20
3 60
5 50
Time Complexity: O(logN), where N is the size of the multimap.
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.
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.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
multimap< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
auto it = mp.find(2);
mp.insert(it, { 3, 60 });
cout << "KEY\tELEMENT\n" ;
for ( auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first << '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output
KEY ELEMENT
1 40
2 30
3 60
Time Complexity: O(logn)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Sep, 2023
Like Article
Save Article