How to create a std::map of constant values which is still accessible by the [] operator?
问题 I need a std:map data structure that is read only, which means I have to fill it once with data and then only read those values, never change them or add additional ones. My non-const version looks like this: //in .h #include <string> #include <map> std::map<std::string, int> myMap; void initMap(); //in .cpp #include "foo.h" void initMap() { myMap["Keys"] = 42; } Then I'd call initMap() once in my code and be done. Now I've read several questions here already and it seems non-trivial to