1.源码实现
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string, map<string, string> > a;
map<string, string> c;
map<string, map<string, string> >::iterator p2;
map<string, string>::iterator p3;
string temp;
char num[10];
int i;
c.clear();
//将3个元素插入到map c中
for(i = 0; i < 3; i++)
{
sprintf(num, "%d", i);
temp = string(num);
c.insert(make_pair(temp, "string"));
}
//将含有3个元素的 map c 插入到 map a中
a.insert(make_pair(temp, c));
//删除二维map中的数据
a["2"].erase("1");
//修改二维map中的数据
a["2"]["2"] = "hello";
c.clear();
//遍历二维map
for(p2 = a.begin(); p2 != a.end(); p2++)
{
for(p3 = p2->second.begin(); p3 != p2->second.end(); p3++)
{
cout << "[" << p2->first << ", " << p3->first << "]: " << p3->second << endl;
}
}
return 0;
}
2.编译源码
$ g++ -o example example.cpp
3.运行及其结果
$ ./example
[2, 0]: string
[2, 2]: hello
网友评论