map and multimap key_type , mapped_type and value_type


In STL container map and multimap,the type of the key ,value and a it’s internal pair structure type are given an alias.These alias are public and they are used by the container itself to simply the type name or we can also use them in our program to declare an object of their type.

Suppose the map and multimap template are declared as,

template<typename Key , typename T , typename Compare= less<Key> ,
typename Allocator= allocator<pair< const Key , T>> >
class map;

template<typename Key , typename T , typename Compare= less<Key> ,
typename Allocator= allocator<pair< const Key , T>> >
class multimap;

Their type alias name is defined as.

typedef key key_type;key_type is a typedef of key type,meaning it signify the type of key.
typedef T mapped_type;mapped_type signify the type T.
typedef pair<const Key , T> value_type;value_type can be used as pair type that has Key and T as it’s first and second data member type.

A detail explanation of each alias is given below.



key_type

key_type is an alias name for the key type in map and multimap container.Do not get confuse if you see key_type,it simply means the type of the key.

Suppose you have declared a map or multimap type ,you can append the key_type with the scope resolution operator(::) to it.Now the template will be interpreted as the type of the key.And any object declared is an object of the key type not an object of map or multimap.Consider the code example below.

map<int , string> mp{{34 ,”string”}} ; ///ok fine

map<int,string>::key_type mp1=908; ///ok fine

map<string,int>::key_type mp2{{“new” , 89 }}; //error, mp2 is int type not map<strong,int > type

cout<< mp1 ; ///This gives 908

In the second map declaration adding ‘::key_type‘ reduce the map<int,string> to int type since the key type is int type.And so we perform normal integer value assignment to the variable/object.In the third declaration we tried to assign a list initializer to string type,to which of course the compiler complains.

multimap<string , string>::key_type mlmp=”String type” ; ///ok fine

multimap<double , string>::key_type mlmp1=”908″ ; ///error assigning string to double type

map<char,int>::key_type mlmp2=’K’ ; //work fine

cout<< mlmp ; ///This gives “String type”






mapped_type

The mapped_type is an alias for the value type of the map and multimap container.Like the key_type if we append this name to the map or multimap declaration with a scope resolution(::) operator in between,the map or multimap is reduce to the type of the value type.Declaring any object will belong to the value type and it is not a map or multimap object.

map<string,string>::mapped_type mp ={{“string”, “string”}} ///error

map<string,int>::mapped_type db=234 ; //work fine

map<double,string>::key_type db=890.e4 ; //work fine

map<char,string>::mapped_type ms=”23.45″ ; //work fine

In the first declaration assigning a list initializer to mp is an error.Adding ‘::mapped_type‘ has changed the type of map<string,string> to string type.The remaining declaration works in the same way and so you may notice the difference.

Here is another code example using multimap.

multimap<char,string>::mapped_type mlmp={{‘B’, “New”}} ;//error

multimap<char,string>::mapped_type mlmp1=”value type” ; //work fine/span>

multimap<string,double>::mapped_type mlmp2=345.e12; ///work fine


 


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


value_type

The map and multimap container utilizes pair structure to manage every single elements;that is also why we can use key/value pair as as single element in map and multimap container.The value_type is an alias of the pair structure with key and T as the type of the first and second data members.

Link : template pair struct

Like the key_type and mapped_type appending value_type with scope resolution(::) operator to the map and multimap type,the type is interpreted as pair type.Since the type is pair type we require an initializer list to initialized the object.

map<string,int>::value_type mvt={{“Value” , 23} , { “type” , 789} }; ///error

map<string,int>::value_type mvt1{“value_type” , 90 }; ///work fine

cout<< mvt1.first << endl
<< mvt1.second ;

Output

value_type
90

A code example using value_type in multimap container.

multimap<double ,string>::value_type mlvt={34.65 , “pair” } ; ///work fine

cout<< mvt1.first << endl
<< mvt1.second ;

Output

34.65
pair


 


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

A code example for all the type alias:key_type,mapped_type and value_type.

map< string,double>::mapped_type db=6.023e23 ;

map<int,char>::value_type pIC{ 13 , ‘I’ };

map<char,string>::key_type c=pIC.second ;

cout<< db << endl
<< pIC.first << ” ” << endl
<< c ;

Output

6.023e23
13 I
I