Is there any way to prevent insert or erase on STL unordered_map?

I often use unordered_maps with fixed / persistent keys but mutable values. Example. If you have one enum Dimension{ X, Y }, you can save a data point for everyone, but never allow insertion or deletion for a map. Updates are fine.

Initialization Example:

typedef std::unordered_map<Dimension, std::size_t> Dimension_To_Size_Map;
// assume std::hash has template specialisation for enum
Dimension_To_Size_Map dimension_To_Size_Map =
    { { Dimension.X, 0 }, { Dimension.Y, 0 } };
dimension_To_Size_Map[Dimension.X] = 12;  // update is ok
dimension_To_Size_Map[Dimension.Y] = 17;  // update is ok
dimension_To_Size_Map[(Dimension)7] = 22;  // insert not allowed
dimension_To_Size_Map.erase(Dimension.X);  // erase not allowed

Can I prevent insertion or erasure, but allow the update, on the STL unordered_map?

One idea: copy, rename and modify the existing implementation unordered_mapto remove insertand erase.

+4
source share
2 answers

std::vector of pair<const K, V> s. [] find begin , .

map , , . , .

+3

std:: map std:: map, .

+4

All Articles