Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 928
andrew Avatar asked Mar 11 '23 04:03

andrew


2 Answers

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.

like image 155
Bathsheba Avatar answered Mar 21 '23 04:03

Bathsheba


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

like image 40
Holt Avatar answered Mar 21 '23 04:03

Holt