map::insert function


The map::insert function allows you to insert a pair element to the map container.There are six version of insert functions.

1 pair<iterator, bool> insert(const value_type& x);
2 template<typename pair >
pair<iterator, bool> insert(pair&& x);

(There is a second template argument type which is implementation-defined)
3 void insert(initializer_list<value_type> ls);
4 iterator insert(const_iterator pos, const value_type& x);
5 template<typename pair>
iterator insert(const_iterator pos , pair&& x);

(There is a second template argument type which is implementation-defined)
6 template<it>
void insert(it first, it last);

When is each of these function call? is discussed below.



pair<iterator, bool> insert(const value_type& x);

This function inserts data to the map container.If the element with the same key already exists in the map then nothing is inserted into the map.

Parameters:
x – A pair type object or pair elements that is to be inserted into the map.

Return type
pair<iterator, bool> -The first data is of map::iterator type that points to the current inserted data.The second value is of bool type.If the insertion process is successful the second value is true else false.

Note if the inserted data is a pair object,the type should match.

Code example

map<int,string> mp;

mp.insert( {12,”String”} ) ;///Work fine

pair<int,string>pr{9,”New”};

mp.insert(pr); ///Work fine

mp.insert(2 , 56); //error,type does not match

cout<< mp[12] << endl
<< mp[9] ;

Output

String
New

Another code example is given below to check the return type of the function.

Code example:Checking the return type

pair<map<int,string>::iterator , bool> prIt=mp.insert( {3 , “New string”} );

cout<< prIt.first->first << ” ” << prIt.first->second << endl
<< prIt.second << endl;

prIt=mp.insert({12 , “Look” });

cout<< prIt.first->first << ” ” << prIt.first->second << endl
<< prIt.second ;

Output

3 New string
1
12 String
0

For the second insert call the returned pair has second value as 0(false).This means the insertion process failed.






template<typename pair >
pair<iterator, bool> insert(pair&& x);

The second version is same as the first function ,except the second version is called when you try to move the object instead of copying the object.

Parameters:
x – A lvalue pair type object or pair elements that is to be inserted into the map.

Return type
pair<iterator, bool> -The first data is of map::iterator type that points to the current inserted data.The second value is of bool type.If the insertion process is successful the second value is true else false.

To call this version you must use the std::move function to move the the lvalue.The std::move function argument can be only a pair object type.So if you are inserting a pair values instead of pair type object then you must call the make_pair function first.The make_pair will convert the pair value to pair type object.

Link:pair make_pair function

Code example

map<int,string> mpNew{{90, “Start trek”}};

mpNew.insert(std::move(make_pair(4,”Movie”))); //work fine

mpNew.insert(std::move({2, “String”})) ; //error

pair<int,string> pr{8,”Star Trek”};

mpNew.insert(std::move(pr)); ///work fine

cout<< mpNew[4] << endl
<< mpNew[8] ;

Output

Movie
Star Trek

Here is another code example that utilize the pair object returned by this function.

pair<map<int,string>::iterator ,bool> pr=mpNew.insert(std::move( make_pair(5,”The irony”)));

cout<< pr.first->first << ” ” << pr.first->second << endl
<< pr.second ;

Output

5 The irony
1



void insert(initializer_list<value_type> ls);

When the argument passed is an intializer list-a list of pair values,this function is called.

Parameters:
ls -An initializer list.

Return type
void

We can say that this version of insert function allows insertions of many pair values into the map container.

Code example

map<double,char> mDC;

mDC.insert( { {3.14 , ‘P’ } , { 6.023e23 , ‘A’} , { 1 , ‘u’ } } );

for(auto elem:mDc)
{
cout<< elem.first << ” ” elem.second << endl ;
}

Output

1 u
3.14 P
6.023e+23 A

Link: C++11 Range-based for loop


 


Stop wasting time,earn money($$$) from your website-Join Now!


iterator insert(const_iterator pos, const value_type& x);

It insert a pair values or object into the map container.And note this function is not concerned whether the insertion took place or not.

Parameters:
pos – const_iterator type that provides a hint to the compiler as to where the object must be inserted.

x :The pair value or object to be inserted.

Return type
iterator -An iterator that points to the current inserted pair.

The first argument which is a hint to the compiler increase the efficiency of the insertion process if the hint is meaningful.A bad hint does not increase the efficiency in any way.

If the insertion process failed the returned iterator point to the pair having the same key as the key of the inserted pair.

Code example

map<string,string> mpSS {{ “SOS” , “Help”}, { “LOL” , “DxD” }};

map<string,string>::iterator posIt=mpSS.begin();

++posIt; //points to second position

mpSS.insert( posIt , {“Q” , “Quote”} ); ///posIt is a good hint

++posIt; //points to third position

mp.insert( posIt , {“AB”, “ZX”} ) ; ///posIt a bad hint

In the second insert call “Q” will come between ‘LOL’ and ‘SOS’-sorting lexicographically- and since posIt points to second position,it is a good hint here.But in the third insert call “AB” will come at first,but posIt points to third position,undeniably it is bad hint here.

Another code example that illustrate the returned iterator pointing position.

map<string,string>::iterator mpIt=mpSS.begin() ,
mpRet=mpSS.insert( mpIt , {“Due” , “Drop”} );

cout<< mpRet->first << ” ” << mpret->second << endl

mpRet=mpSS.insert( mpIt , {“SOS” , “Son Oh Son”} );

cout<< mpRet->first << ” ” << mpret->second << endl

Output

Due Drop
SOS Help

In the second insert call no insertion takes place since a pair with the key “SOS” is already present in the map .The returned iterator also points to the pair with “SOS” as the key.






template<typename pair> iterator insert(const_iterator pos , pair&& x);

This overloaded version is same as the 4th version-the previous one- except this version is called if you try to move the object.

Parameters:
pos – const_iterator type that provides a hint to the compiler as to where the object must be inserted.

x :A lvalue pair value or object to be moved.

Return type
iterator -An iterator that points to the current inserted pair.

If the iterator serve as a good hint the efficiency is increased else no increase in efficiency.The returned iterator points to the current pair if the insertion is successful.If insertion fails it points to the existing pair with the same key.

Here since we are trying to move the pair we should use std::move function and also note call make_pair first if you are moving a pair initializer.

map<double,string> mDS{{23 , “twenty three”} , { 9 , “Nine” }};

map<double,string> ::iterator it=mDS.begin() ;

mDS.insert( it, std::move(make_pair({ 11 , “eleven” })) );

cout<< mDS[11] ;

Output

eleven

Another example to check the returned iterator.

map<double,string> ::iterator mDSRet=mDS.insert( it, std::move(pair<double,string>{10, “1 and 0”}) ); //work fine

cout<< mDSRet->first << ” ” << mDSRet->second ;

Output

10 1 and 0


 


Stop wasting time,earn money($$$) from your website-Join Now!


template<it> void insert(it first, it last);

This last overloaded version of insert function will insert a range of pair value taken from another map or multimap container or a container with pair elements.

Parameters:
first – A starting point of the range.

last -The end point of the range.

Return type
void

In this function call there is no way of knowing if the call succeeds or fail.The only of knowing for successful call is to check them explicitly.

Code example

map<char , int> m1{{‘k’ , 67 } , { ‘A’ 65 }} , m2;

m2.insert( m1.begin() ,m1.end() );

for(auto elem:m2)
{
cout<< elem->first << ” ” << elem->second << endl;
}

Output

A 65
k 67


Conclusion

If you want to assign a new data to the second element of the pair then use the function insert_or_assign.This function will either insert or assign the inserted value to the second element of the pair if a pair with the inserted key is found in the container.