map::operator= assignment function


The map::operator= assignment function can assign the elements of one object to another.There are three versions of operator= assignment function:

1 map& operator=(const map& x);
2 map& operator=(const map&& x) =default;
3 map& operator=(initializer_list<value_type> l);

value_type:it is an alias for pair<key_type,mapped_type> type.

Link:key_type of pair

Note::in calling operator assignment function the allocator object is not copied only the elements are copied.


map& operator=(const map& x);

This version is the simplest version of operator= function and we often use it in out program.Is simply copies one object to another.

Parameters:
x -The object that is to be copied to another object.

Return type
map& -A pointer to this.So,the return type is *this.

Note if the object holds any elements it will be overwritten.

Code example

map<int,string> mp{{2,”Two”} , { 78 ,”Seventy-eight”} , {90,”Ninety”}} , mp1 ;

mp1=mp; //calls the first version

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

Output

2 Two
78 Seventy-eight
90 Ninety


 


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


map& operator=(const map&& x) =default;

This overloaded version is called when you try to move the object instead of copying the object.

Parameters:
x -A lvalue to be moved.

Return type
mpa& – A *this type.

To move the object use the function std::move,without calling this function you cannot move the object.And if the object holds any data assigning new object will overwrite it.

The =default tells the compiler to synthesize the function’s definition for us.You can read more about =default in the link provided below.

LinK:C++11 ‘=default’ keyword

Code example

map<char,int> m{{‘%’ , 55}, {‘&’ , 77} , { ‘*’,88} , {‘(‘ , 99} } ,
m1{ {‘@’,22} , {‘^’,66} };

m1=std::move(m); //calls this version

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

Output

% 55
& 77
( 99
* 88

The elements of m1 is overwritten.






map& operator=(initializer_list<value_type> l);

You can use this function if you want to assign elements from initializer_list.The type is a value_type which is a type definition of pair&lt,key_type,mapped_type>.

Link:value_type

Parameters:
l -An initializer_list object of type pair&lt,key_type,mapped_type>.

Return type
map& -A *this type.

Note,the type of the initializer_list must be value_type,it must not be pair<key_type,mapped_type> .Although value_type is an alias for pair<key_type,mapped_type> an initializer_list object with that type cannot used for assignment to the map object.The initializer_list object must be map<key_type,mapped_type>::value_type type.

If the object has some elements then assigning a new object will overwrite the previous elements.

Code example

initializer_list< pair<string,int> > il{{“aa” , 9797} , {“ab”, 9798} };

initializer_list< map<string,int>::value_type > l{{“23” , 23} , { “44” , 44} , {“21”, 21} };

map<string,int> mp ,mp1;

mp=l; //work fine
/*mp1=il; //error

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

Output

21 21
23 23
44 44

Assigning il to mp1 is an error.There is a discrepancy in there types.