The map::insert() is a built-in function in C++ STL which is used to insert elements with a particular key in the map container.
iterator map_name.insert({key, element})
Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the map container. The function does not insert the key and element in the map if the key already exists in the map.
Return Value: The function returns a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the element with an equivalent key in the map. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent key already existed.
Below is the illustration of the above syntax:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< 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
3 60
5 50
iterator map_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 map container.
- position: This does not specify the position where the insertion is to be done, it only points to a position from where the searching operation for insertion is to be started to make the process faster. The insertion is done according to the order which is followed by the map container.
Return Value: The function returns an iterator pointing to the new element in the container.
Below is the illustration of the above syntax:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< 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
iterator map_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 another map container.
Return Value: The function returns an iterator pointing to the new element in the container.
Below is the illustration of the above syntax:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > mp, mp1;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp1.insert(mp.begin(), mp.end());
cout << "Elements in mp1 are\n" ;
cout << "KEY\tELEMENT\n" ;
for ( auto itr = mp1.begin(); itr != mp1.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output:
Elements in mp1 are
KEY ELEMENT
1 40
2 30
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...