VOOZH about

URL: https://www.geeksforgeeks.org/cpp/map-insert-in-c-stl/

⇱ map insert() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

map insert() in C++ STL

Last Updated : 11 Jul, 2025

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:


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


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


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


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


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.

Comment