I need just dictionary or associative array string
=> int
.
There is type map C++ for this case.
But I need only one map forall instances(-> static) and this map can't be changed(-> const);
I have found this way with boost library
std::map<int, char> example =
boost::assign::map_list_of(1, 'a') (2, 'b') (3, 'c');
Is there other solution without this lib? I have tried something like this, but there are always some issues with map initialization.
class myClass{
private:
static map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
static map<int,int> myMap = create_map();
};
The C++11 standard introduced uniform initialization which makes this much simpler if your compiler supports it:
//myClass.hpp
class myClass {
private:
static map<int,int> myMap;
};
//myClass.cpp
map<int,int> myClass::myMap = {
{1, 2},
{3, 4},
{5, 6}
};
See also this section from Professional C++, on unordered_maps.