map::value_comp function

The map::value_comp function returns the value comparison object.

value_compare value_comp() const;

value_compare:A class member of map that compares the key of the pair element.


Return type
value_compare -A an object of value_compare class which compares the key of the pair.

There is a class member of the map container known as value_compare.This class has no specific implementation.It simply calls the key comparison criteria.Key is the main factor to be taken into account when sorting the elements.

Code example

map<char,string> mp; {{ ‘a’ , “am” } , { ‘p’ , ‘pm’ }};
map<char,string>::iterator it=mp.begin() , it1=++(mp.begin());

map<int,string>::value_compare vl=mp.get_comp();

cout<< vl( {it->first , it->second} , {it1->first,it1->second} ) ; /*calls the operator() of value_compare class*/



Link:value_compare::operator() function

If the user uses their own criteria the value_compare also belongs to that type.So ,the return type of value_comp should be assigned to the object which implement the user defined criteria.

Code example

map<char,int,UserCriteria> m{{‘C’ , 67} , {‘K’ , 90}};

map<char,int,UserCriteria>::value_compare vc=m.value_comp(); //work fine

map<char,int>::value_compare vc1=m.value_comp(); ///error,vc1 does not implement ‘UserCriteria’

On how to change the criteria of the map container you can visit the link given below.

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


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