Can I construct an std::map
where the key type is a reference type, e.g. Foo &
and if not, why not?
We can use any of the data types as the data type of the key of the map. Even a user-defined data type can be used as key data type.
Yes -- when you insert an item into an std::map, you pass it by value, so what it contains is a copy of what you passed.
List: A list in STL is used to implement the doubly-linked lists. Unlike an array, lists are used to store the data, which is not contiguous.
According to C++ Standard 23.1.2/7 key_type
should be assignable. Reference type is not.
No, because many of the functions in std::map takes a reference to the keytype and references to references are illegal in C++.
/A.B.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With