# map insert() in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of map::insert() function in C++ STL.

## What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

## What is a map::insert()?

map::insert() function is an inbuilt function in C++ STL, which is defined in  header file. insert() is used to insert new values to the map container and increases the size of container by the number of elements inserted.

As the keys in the map container are unique, the insertion operations check whether each element which is to be inserted has a key which is already existing in the container, if yes then the element is not inserted.

Also, the map container maintains all their elements via their respective key in the ascending order. So, whenever we insert an element it goes to its respective position according to its key.

## Syntax

1. Map_name.insert({key& k, value_type& val});
or
2. Map_name.insert(iterator& it, {key& k, value_type& val});
or
3. Map_name.insert(iterator& position1, iterator& position2);

## Parameter

This function accepts the following parameter

• k − This is the key which is associated with the element. The function checks if the key is already in the container then it doesn’t insert the element.

• val − The value which is to be inserted.

• it − The iterator type of value which is used to give the position where we want the element to be inserted.

• position1, position2 − The position1 is the starting position and position2 is the ending position when we want to insert the series of elements, we can use the range of multiple elements which we want to insert.

## Return value

This function returns an iterator to the element newly inserted in the map container.

## Input

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.insert({d, 50});

## Output

a:1
b:2
c:3
d:50

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP_Map;
TP_Map.insert({3, 50});
TP_Map.insert({2, 30});
TP_Map.insert({1, 10});
TP_Map.insert({4, 70});
cout<<"TP Map is : \n";
cout << "MAP_KEY\tMAP_ELEMENT\n";
for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {
cout << i->first << "\t" << i->second << endl;
}
return 0;
}

## Output

TP Map is:
MAP_KEY    MAP_ELEMENT
1          10
2          30
3          50
4          70

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP_Map;
TP_Map.insert({3, 50});
TP_Map.insert({2, 30});
TP_Map.insert({1, 10});
TP_Map.insert({4, 70});
auto i = TP_Map.find(4);
TP_Map.insert(i, { 5, 80 });
cout<<"TP Map is : \n";
cout << "MAP_KEY\tMAP_ELEMENT\n";
for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {
cout << i->first << "\t" << i->second << endl;
}
return 0;
}

## Output

TP Map is:
MAP_KEY    MAP_ELEMENT
1             10
2             30
3             50
4             70
5             80
Published on 15-Apr-2020 12:38:28