Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to make std::map key const? [duplicate]

Tags:

c++

constants

map

When defining a std::map, does it make sense to make the key type const?

like image 753
Graeme Avatar asked Jun 10 '11 13:06

Graeme


People also ask

Can you have duplicate keys in a map C++?

C++ Software Engineering Multi-map in C++ is an associative container like map. It internally store elements in key value pair. But unlike map which store only unique keys, multimap can have duplicate keys.

Does std :: map insert make copy?

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.

Can a map have two keys with the same value C++?

This is why every key has to be unique, and no two keys can be the same(but the values associated to keys can be the same). Maps in C++ store the key-value pairs in sorted order by default so that the search for any key-value pair can be very quick.

Are map keys unique C++?

All the elements of the set are unique. A map data structure in C++ is a structure in which the list is a hash of key/value pairs, stored in ascending order of keys by default, or in descending order of keys by programmer's choice. The keys are also unique, and there can be duplicated values.


1 Answers

The key is already const, actually, because a key cannot change once an element was inserted in the map.

like image 133
Etienne de Martel Avatar answered Nov 14 '22 23:11

Etienne de Martel