C++ map insert() Function

Last Updated : 15 May 2026

map insert() Function

In C++, the map::insert() function is used to insert new elements into the map container. Since a map stores unique keys, the insertion operation first checks whether the key already exists in the map. If the key is already present, the insertion fails; otherwise, the new key-value pair is inserted into the map in sorted order.

C++ map insert() Function

Syntax

It has the following syntax.

Parameters

  • val: It represents a key value to insert in the map.
  • position: It represents the hint for the position to insert the element.
  • first: It represents the beginning of the range to insert.
  • last: It represents the end of the range to insert.
  • il: It represents an initializer list.

Return Value

It returns a bool pair to indicate whether insertion has happened or not, and returns an iterator pointing to the newly inserted element.

Examples of map insert() Function

Here, we are going to discuss several examples to demonstrate the map insert() Function

Why Use the map::insert() Function in C++?

There are several scenarios where we can use the insert() function in C++. Some of them are as follows:

  • In the C++ programming language, the map::insert() function will only insert if the key does not already exist. If the key exists, the insertion will fail, and the old value will be preserved.
  • The map insert() function provides the details whether the insertion took place or not, and where, if it did. It can also be useful for checking for duplicates.
  • This function also provides the hint and range-based overloads that are effective for insertion operations.

Example 1: Insert Elements into a Map Using map::insert()

This example demonstrates how to insert new key-value pairs into a map using the insert() function and display the elements in sorted order.

Output:

The map contains the following elements:
a = 1
b = 2
c = 3
d = 4
e = 5

Explanation:

In this example, we have created a map (m) that is initialized with three key-value pairs. After that, we have utilized the insert() function to include new elements ('d', 4 and 'e', 5) in the map container. Next, we utilize the for loop that iterates via the map and prints all the key-value pairs in sorted order.

Example 2: Insert Elements with a Hint Position

This example demonstrates how to insert elements into a map using a hint iterator to optimize the insertion position.

Output:

The map contains the following elements:
a = 1
b = 2
c = 3
d = 4
e = 5

Explanation:

In this example, we have created a map m and initialized three key-value pairs in it. After that, we have included new elements in the defined position, i.e., in the beginning element {'a', 1} is inserted, and in the end element {'e', 5} is inserted. Finally, it iterates via the program and prints all key-value pairs in a sorted order.

Example 3: Insert a Range of Elements into Another Map

This example demonstrates how to copy and insert a range of elements from one map container into another map using the insert() function.

Output:

The map contains the following elements:
a = 1
b = 2
c = 3
d = 4
e = 5

Explanation:

In this example, we have created a map m1 that contains five elements, and map m2 is empty. After that, we have utilized the insert() function to insert the elements of m1 to m2 from the beginning of m1 to the end of m1 and print the data of the m2 map.

Example 4: Use Different Methods of map::insert()

This example demonstrates different ways to use the map::insert() function, including duplicate key insertion, hint-based insertion, initializer list insertion, and range insertion from another map container.

Output:

Initial Student Records:
101 : Johnson
102 : Peter
103 : Robert

--- Attempting Duplicate Key Insertion ---
Duplicate key '103' not inserted. Existing value: Robert

--- Hint-Based Insertion ---
Inserted element 105 using hint.

--- Batch Insertion Using Initializer List ---
Multiple elements inserted successfully.

--- Inserting Elements from Another Map ---
Copied all elements from newStudents map.

--- Final Map Contents ---
101 : Johnson
102 : Peter
103 : Robert
105 : Alisha
106 : Farhan
107 : Gauri
108 : Harsh
201 : Ira
202 : Jay
203 : Kavya

--- Summary ---
Total Students in the Map: 10
Is key 103 present: Yes
Is key 110 present: No

Explanation:

In this program, we create multiple data types using the map::insert() function. First, it shows the duplicate key insertion (key 103) because maps contain only unique keys. After that, we use a hint-based insertion that includes a new element efficiently, which is followed by batch insertion using an initializer list that includes multiple entries at once. Finally, elements are inserted from another map using a range insertion, and then the complete map contents, total size, and key existence are displayed on the console.


Next TopicC++ Map