map::key_comp function


The map::key_comp function returns the criteria used by the map container to sort the key of the element in the pair.

key_compare key_comp() const;

Key_compare:A sorting criteria used by the map container.

Parameters:
none

Return type
key_comp -A sorting criteria/algorithm used by the map container.

Note the default criteria used by the map container is less<key_type>.This criteria sorts the elements in the ascending order of the keys of the pair elements.If you implement your own criteria the return type if the criteria you have implemented.

Link:key_type of pair element

Code example

map<int,string> mp{{34 ,”3535″} , { 9 , “Nine”}};

less<int> ls=mp.key_comp( );

Since default less<int> is the algorithm used the code above work fine.

Here is another example demonstrating the return type when we implement our own criteria.

Code example

map<int,string,UserCriteria> m{{12 , “New”} , {90 , “ok”} , {45 , “Truth”}};

UserCriteria uc=m.key_comp( ); //work fine

If the programmer uses their own criteria the key_comp return type is the user-defined criteria.

You can check out the link provided below on how to change the criteria of the associative container.

Link:changing the sorting criteria/algorithm of STL containers -map , multimap , set ,etc


 


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