Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there key_type and value_type in std::set

Tags:

c++

set

After reading this link: https://en.cppreference.com/w/cpp/container/set, I just found that there were two defined types: key_type and value_type in the class std::set. It seems that they are exactly the same thing.

Well, this may be a stupid question but I still want to ask why. Isn't one enough? Why are there two types?

like image 204
Yves Avatar asked Nov 01 '25 23:11

Yves


1 Answers

All the stl containers have value_type, and all the associative containers (including std::set, std::map, std::multiset, std::multimap) and unordered associative containers (including std::unordered_set, std::unordered_map, std::unordered_multiset, std::unordered_multimap) have key_type, that means you can perform some general processing (with templates especially) on these containers with the member types.

like image 153
songyuanyao Avatar answered Nov 03 '25 14:11

songyuanyao