Open In App

map insert() in C++ STL

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::map::insert() is a built-in function of C++ STL map container which is used to insert new elements into the map. In this article, we will learn how to use map::insert() function in our C++ programs.

Example:

C++
// C++ program to illustrate how to use
// map::insert
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, string> m;

    // Insert single elements in random order
    m.insert({1, "one"});
  
  	// Insert multiple elements
    m.insert({{2, "two"}, {4, "four"}});

    for (auto i : m)
        cout << i.first << ": " << i.second << '\n';
    return 0;
}

Output
1: one
2: two
4: four



Syntax

The map::insert() function provides 4 different overloads:

m.insert({k, v}) // For single element
m.insert(pos, {k, v}) // For single element near pos
m.insert({ {k1, v1}, {k2, v2}, ….}); // For multiple elements
m.insert(first, last); // For range

We can use these overloads for different ways to insert elements in std::map() in C++:

Insert Single Element

We can use the map::insert() to insert a key value pair in the map. It will be inserted at the position according to the order of the map elements.

Syntax

m.insert({k, v})

Parameters

  • {k, v}: Key-value pair to be inserted.

Return Value

  • Returns a pair, where pair::first is an iterator pointing to either the newly inserted element or to the element with same key in the map.
    The pair::second is a boolean value that tells whether the insertion was successful or not.

Note: As map contains only unique key value pairs, map::insert() function won’t insert the elements that is already present in the map.

The insert() function is essential for adding elements to a map.

Example

C++
// C++ program to illustrate how to use
// map::insert to insert a single element
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, string> m;

    // Insert single elements in random order
    m.insert({1, "one"});
    m.insert({4, "four"});
    m.insert({2, "two"});

    // Trying to insert duplicate key
    m.insert({2, "TWO"});

    for (auto i : m)
        cout << i.first << ": " << i.second << '\n';
    return 0;
}

Output
1: one
2: two
4: four

Time Complexity: O(log n), where n is the number of elements.
Auxiliary Space: O(1)

Insert Element Near the Given Position

We can also use the map::insert() function to insert the key-value pair near the given position. std::map has to maintain the order of elements. We cannot force the insertion at any particular position, so the given position only gives a hint to the map::insert() function as from where to start searching for the place to insert.

Syntax

m.insert(pos, {k, v})

Parameters

  • {k, v}: Key-value pair to be inserted.
  • pos: Iterator to the position near which the new element is to be inserted.

Return Value

  • Returns an iterator pointing to either the newly inserted element or to the element with same key in the map.

Example: Inserting an Element Near Given Position

C++
// C++ program to illustrate how to use
// map::insert to insert the given pair
// near some specific position
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, string> m;
    m.insert({1, "one"});
    m.insert({4, "four"});
	
  	// Finding the position to insert
    auto i = m.find(4);

    // Inserting {3, 60} starting the search
    // from position where 2 is present
    m.insert(i, {2, "two"});

    for (auto i : m)
        cout << i.first << ": " << i.second << '\n';
    return 0;
}

Output
1: one
2: two
4: four

Time Complexity: O(log n)
Auxiliary Space: O(1)

Insert Multiple Elements

We can insert multiple key value pairs in a map using map::insert() by enclosing the multiple key value pairs inside the braces {} and separate each of them using a comma. This form is called initializer list.

Syntax

m.insert({ {k1, v1}, {k2, v2}, ….});

Parameters

  • {k1, v1}, {k2, v2}…: First pair, second pair and so on inside { } braces.

Return Value

  • This function does not return anything.

Example

C++
// C++ program to illustrate how to use
// map::insert to insert multiple key-value
// pairs using initializer list
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, string> m;

    // Insert multiple key-value pairs using
  	// initializer list
    m.insert({{1, "one"}, {2, "two"}, {4, "four"}});

    // Display the elements of the map
    for (auto i : m)
        cout << i.first << ": " << i.second << '\n';
    
    return 0;
}

Output
1: one
2: two
4: four

Time Complexity: O(k log n), where n is the number of elements already present in the map.
Auxiliary Space: O(k), where k is the number of elements to be inserted.

Insert Elements from Given Range

The map::insert() function can also be used to insert elements from the given range. This range can by any STL container or an array.

Syntax

m.insert(first, last);

Parameters

  • first: Iterator to the first of the range.
  • last: Iterator to the element just after the last element of the range.

Return Value

  • This function does not return anything.

Example

C++
// C++ program to illustrate how to use
// map::insert to insert multiple elements
// from a given range of an STL container
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, string> m;
    
    // Vector of pairs to insert into the map
    vector<pair<int, string>> v = {{1, "one"},
                    {2, "two"}, {4, "four"}};
    
    // Insert elements from vector
    m.insert(v.begin(), v.end());
    for (auto i : m)
        cout << i.first << ": " << i.second << '\n';
    return 0;
}

Output
1: one
2: two
4: four

Time Complexity: O(k log n), where n is the number of elements in the map.
Auxiliary Space: O(k), where k is the number of elements in the range.
 



Next Article

Similar Reads

three90RightbarBannerImg