I need a std: map data structure that is only readable, which means that I have to fill it with data once, and then read only these values, do not 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 would call initMap() once in my code and do it.
Now I have read several questions here, and for the map it seems nontrivial to achieve a constant.
To do this std::map<std::string, const int> will not allow filling it in initMap() . Filling it with a non-constant temp, and the copy constructor by definition, does not work either, since the copy constructor does not just accept the non-constant version as input.
Creating const std::map<std::string, int> (which I could fill with a non-constant copy during the definition) will disable the use of the [] operator to access the values.
So, is there a way to achieve (value) a constant and initialize the structure (preferably in the header file)?
BTW: neither C ++ 0x, nor C ++ 11, nor boost:: are parameters.
source share