map::insert_or_assign function


The map::insert_or_assign function as the name suggest can either insert an element or assign a new data to the value of the existing pair element.There are four version of this function:

1 template <typename Ob>
pair<iterator, bool> insert_or_assign(const key_type& k , Ob&& obj);
2 template <typename Ob>
pair<iterator, bool> insert_or_assign(key_type&& k , Ob&& obj);
3 template <typename Ob>
iterator insert_or_assign(const_iterator hint, const key_type& k , Ob&& obj);
4 template <typename Ob>
iterator insert_or_assign(const_iterator hint, key_type&& k , Ob&& obj);

In calling this function,suppose if an element with the key already exist then the value-the second element of the pair element-is replace with the new value passed.A detail description of each of the functions is given below.



template <typename Ob>
pair<iterator, bool> insert_or_assign(const key_type& k , Ob&& obj);

The function insert the pair element if any pair with the same key does not exist in the container.If a pair with the same key exist,then the second argument will be assigned to the value of the existing pair with the same key.

Parameters:
K -The key value to be checked against the existing key in the container.

obj -The value of the pair element to be inserted in the container.It is assign to the value of the pair element if a key same as the first argument is found.

Return type
pair<iterator, bool> -A pair object whose first element is an iterator of the map type and the second element is a bool type.The iterator points to current inserted data or pair whose value is assigned the new data.The bool value is true if insertion takes place(the data is added),else false.

Let’s look at the code example given below.

Code example

map<char,int > mp{{‘B’ , 1256 }};

mp.insert_or_assign(‘A’ , 890 ); //the data is inserted since no key with ‘A’ exist in the container

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

mp.insert_or_assign(‘B’ , 7584 ); //7584 is assigned to the value of the pair with the key as ‘B’

cout<< mp[‘B’] << endl ; //returns the newly assign value

Output

A 890
B 1256
7584

Here is another code example to test the return type.

Code example

map<char,int>::iterator mpIt=insert_or_assign(‘C’ , 9012 );

cout<<mpIt->first << ” ” << mpIt->second endl ;

Output

C 9012

The ‘mpIt’ points to the current inserted data.


 


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


template <typename Ob>
pair<iterator, bool> insert_or_assign(key_type&& k, Ob&& obj);

This version is same as the first function except it can move the object.To move the object call the function std::move.

Parameters:
k -A lvalue key to be checked for or inserted into the map.

obj -The value of the pair element to be moved into the container.It is assign to the value of the pair element if a key same as the first argument is found.

Return type
pair<iterator, bool> -A pair object whose first element is an iterator of the map type and the second element is a bool type.The iterator points to current inserted data or pair whose value is assigned the new data.The bool value is true if insertion takes place,else false.

A code example is given below.

Code example

map<int,string> mp;

mp.insert_or_assign( std::move(2) , std::move(“Two”) );

cout<< mp[2] ;

Output

Two

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

Code example

pair< map<int,string>::iterator, bool > mpIt=mp.insert_or_assign(std::move(4) , std::move(“Four”) );

cout<< mpIt.first->first << ” ” << mpIt.first->second ;
<< mpIt.second ;

Output

4 Four
1






template <typename Ob>
iterator insert_or_assign(const_iterator hint, const key_type& k , Ob&& obj);

An overloaded version through which we can provide a hint as where to insert or assign the new pair.

Parameters:
hint -An iterator of the map type that provides a hint as where to insert the pair.

k -A key to be checked for or inserted into the container.

obj -The value of the pair element to be inserted in the container.It is assign to the value of the pair element if a key same as the first argument is found.

Return type
iterator -An iterator that points to the current inserted pair or a pair with the same key.

A code example is given below.

Code example

map<string,string> mpSS{ { “Aa” , “Apple” } , { “AA” , “Amnesia” } };

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

mpSS.insert_or_assign( mpIt , “AaA” , “Abaddon” ); //mpIt provide a hint to insert or assign the data at the beginning of the container

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

Output

AA Amnesia
Aa Apple
AaA Abanddon

The hint only serve as a helper to the compiler,the compiler can ignore it.

Here is another code example to check the return type.

Code example

map<string,string>::iterator mpSSIt=mpSS.insert_or_assign( mpIt , “Aa” , “Abacus” ) ;

cout<< mpSSIt->first << ” ” << mpSSIt->second << endl ;

Output,

Aa Abacus

No data is inserted into the map container and so the returned iterator(mpSSIt) points to the existing storage with the same key.


 


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


template <typename Ob>
iterator insert_or_assign(const_iterator hint, key_type&& k , Ob&& obj);

This version is same as the previous one but this one is use for moving the object instead of copying the object.

Parameters:
hint -An iterator of the map type that provides a hint as where to insert the pair.

k -A lvalue key to be checked for or inserted into the container.

obj -The value of the pair element to be moved into the container.It is assign to the value of the pair element if a key same as the first argument is found.

Return type
iterator -An iterator that points to the current inserted pair or a pair with the same key.

Code example

map<int,double> mpID{ {12 , 90.e23} };

map<int,double>::iterator mpIDIt=mpID.begin( );

mpID.insert_or_assign( mpIDIt , std::move(11) , 908.34e12 );

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

Output

11 908.34e12
12 90.e23

Another code example is provided below which illustrate the return type.

Code example

map<int,double>::iterator itRet=mpId.insert_or_assign( mpIDIt , std::move(10) , std::move(120.23) );

cout<< itRet->first << itRet->second << endl ;

Output

10 120.23