Search code examples
c++performancestlstdstdset

set::key_comp vs set::value_comp in C++?


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/


Solution

  • 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.