在C ++中初始化靜態(tài)std :: map <int,int>初始化靜態(tài)地圖的正確方法是什么?我們需要一個初始化它的靜態(tài)函數(shù)嗎?
3 回答

弒天下
TA貢獻1818條經(jīng)驗 獲得超8個贊
使用C ++ 11:
#include <map>using namespace std;map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
使用Boost.Assign:
#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');

ABOUTYOU
TA貢獻1812條經(jīng)驗 獲得超5個贊
最好的方法是使用一個功能:
#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();
- 3 回答
- 0 關(guān)注
- 4630 瀏覽
添加回答
舉報
0/150
提交
取消