4

What is the difference between set::key_comp vs set::value_comp in C++? Going to cplusplus.com page there is no significant difference. Furthermore on set::key_comp & related set::value_comp pages last sentence is "(...) key_comp and its sibling member function value_comp are equivalent."

Examples are almost the same:

http://www.cplusplus.com/reference/set/set/key_comp/

http://www.cplusplus.com/reference/set/set/value_comp/

andrew
  • 3,083
  • 4
  • 24
  • 29
  • 2
    They're the same also better to use [cpp reference](http://en.cppreference.com/w/cpp/container/set) rather than cplusplus IMO. see http://en.cppreference.com/w/cpp/container/set/value_comp and http://en.cppreference.com/w/cpp/container/set/key_comp – EdChum Jan 13 '17 at 09:35

3 Answers3

3

key_comp defines the order of the keys in a container.

value_comp defines the order of the values in a container.

In a std::set where, essentially, the values are the keys, the two are indeed exactly equivalent. But that's not true in all containers, e.g. std::map, or, in general, in a container that you might build yourself that follows the conventions of the C++ Standard Library Containers.

Note also that http://en.cppreference.com/w/ is a superior reference for C++. It pretty much proxies the standards.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

These are identical, both must be made available by any implementation because std::set must meet the requirement of Associative Container.

This allows you to write generic code that works with any Associative Container (std::set, std::map, std::multiset, std::multimap in the standard library).

Holt
  • 36,600
  • 7
  • 92
  • 139
1

The difference comes when key and value are different entities inside a container.

For containers like set, these two terms mean same thing.

While, for containers like map or multimap, the key and value are separate entities maintained as an single entry.

Here is an example which shows how they differ:

std::set<int> myset;
int highest1, highest2, highest3;
typedef map<int, int> MyMap;
MyMap mymap;

std::set<int>::key_compare   myCompKeyForSet = myset.key_comp();
std::set<int>::value_compare myCompValForSet = myset.value_comp();

MyMap::key_compare   myCompKeyForMap = mymap.key_comp();
MyMap::value_compare myCompValForMap = mymap.value_comp();


for (int i=0; i<=5; i++) {
  myset.insert(i);
  mymap.insert(make_pair(i, 2*i));
}

//////SET///////

highest1=*myset.rbegin();
std::set<int>::iterator it=myset.begin();
while ( myCompKeyForSet(*it, highest1) ) it++;
std::cout << "\nhighest1 is " << highest1;  // prints 5


highest2 = *myset.rbegin();
it=myset.begin();
while ( myCompValForSet(*it, highest2) ) it++;
std::cout << "\nhighest2 is " << highest2;   // prints 5

//////MAP///////

MyMap::iterator it2 = mymap.begin();
highest3 = mymap.rbegin()->first;
while ( myCompKeyForMap((it2->first), highest3) ) it2++;
std::cout << "\nhighest3 is " << highest3;     // prints 5

std::pair<int,int> highest4 = *mymap.rbegin();    //must be defined as map's `value_type`
it2 = mymap.begin();
while ( myCompValForMap(*(it2), highest4) ) it2++;  // takes `value_type` which is `pair<int, int>` in this case. 
std::cout << "\nhighest4 is " << highest4.second;   // prints 10

Live demo

As I mentioned the passed arguments to value_compare function object must be of type value_type&, so I am in a kind of disagreement with those saying that these two set::key_comp and set::value_comp are easily compatible across associative containers.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79