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.

It has the following syntax.
It returns a bool pair to indicate whether insertion has happened or not, and returns an iterator pointing to the newly inserted element.
Here, we are going to discuss several examples to demonstrate the map insert() Function
There are several scenarios where we can use the insert() function in C++. Some of them are as follows:
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.
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.
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.
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.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India