初始化静态映射的正确方法是什么?我们是否需要一个静态函数来初始化它?
当前回答
最好的方法是使用函数:
#include <map>
using namespace std;
map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
map<int,int> m = create_map();
其他回答
使用c++ 11:
#include <map>
using namespace std;
map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
使用Boost。分配:
#include <map>
#include "boost/assign.hpp"
using namespace std;
using namespace boost::assign;
map<int, char> m = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd');
最好的方法是使用函数:
#include <map>
using namespace std;
map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
map<int,int> m = create_map();
我将把地图包装在一个静态对象中,并将地图初始化代码放在这个对象的构造函数中,这样你就可以确保在初始化代码执行之前创建了地图。
你可以试试:
std::map <int, int> mymap =
{
std::pair <int, int> (1, 1),
std::pair <int, int> (2, 2),
std::pair <int, int> (2, 2)
};
下面是使用2元素数据构造函数的另一种方式。不需要函数来初始化它。没有第三方代码(Boost),没有静态函数或对象,没有技巧,只有简单的c++:
#include <map>
#include <string>
typedef std::map<std::string, int> MyMap;
const MyMap::value_type rawData[] = {
MyMap::value_type("hello", 42),
MyMap::value_type("world", 88),
};
const int numElems = sizeof rawData / sizeof rawData[0];
MyMap myMap(rawData, rawData + numElems);
既然我写了这个答案,c++ 11就过时了。你现在可以使用新的初始化列表功能直接初始化STL容器:
const MyMap myMap = { {"hello", 42}, {"world", 88} };