map insert() in C++ STL

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 an iterator pointing to the new element in the container. 

Below is the illustration of the above syntax: 

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// map::insert({key, element})
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    map<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
 
    // does not inserts key 2 with element 20
    mp.insert({ 2, 20 });
    mp.insert({ 5, 50 });
 
    // prints the elements
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
             << '\t' << itr->second << '\n';
    }
    return 0;
}
chevron_right

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: 

Return Value: The function returns an iterator pointing to the new element in the container.

Below is the illustration of the above syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// map::insert(iteratorposition, {key, element})
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
   
    // initialize container
    map<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
 
    auto it = mp.find(2);
 
    // inserts {3, 60} starting the search from
    // position where 2 is present
    mp.insert(it, { 3, 60 });
 
    // prints the elements
    cout << "KEY\tELEMENT\n";
    for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
        cout << itr->first
            << '\t' << itr->second << '\n';
    }
    return 0;
}
chevron_right

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: 

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// map::insert(iteratorposition1, iteratorposition2)
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    map<int, int> mp, mp1;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
 
    // inserts all elements in range
    // [begin, end) in mp1
    mp1.insert(mp.begin(), mp.end());
 
    // prints the elements
    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;
}
chevron_right

Output: 
Elements in mp1 are
KEY    ELEMENT
1    40
2    30



 

 

 

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





The function of education is to teach one to think intensively and to think critically

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.



Improved By : arorakashish0911

Article Tags :
C++
Practice Tags :