我有一個嵌套映射,即map<int, map<int, string>>
,我想用初始化列表進行初始化。我可以使用初始化列表來初始化單層地圖,但似乎無法找出嵌套地圖的正確語法。它甚至有可能嗎?C++ 11帶列表初始化的嵌套映射
MWE:
// This example shows how to initialize some maps
// Compile with this command:
// clang++ -std=c++11 -stdlib=libc++ map_initialization.cpp -o map_initialization
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
cout << "\nLearning map initialization.\n" << endl;
map<int, string> level1map = {
{1, "a"},
{2, "b"},
{3, "c"}
};
for (auto& key_value : level1map) {
cout << "key: " << key_value.first << ", value=" << key_value.second << endl;
}
// This section doesn't compile
// map<int, map<int, string>> level2map = {
// {0,
// {0, "zero"},
// {1, "one"},
// {2, "two"}
// },
// {1,
// {0, "ZERO"},
// {1, "ONE"},
// {2, "TWO"}
// }
// };
return 0;
}