|
蓝森林 http://www.lslnet.com 2006年6月6日 10:18
c++里面怎样才能实现这样的数据结构
就是类似map, 但是里面的key是二维的
例如说
a["this"]["this"]=1;
a["this"]["that"]=2;
a["that"]["this"]=3;
a["that"]["that"]=4;
有什么办法吗? |
我能想到的就是自己实现一个map然后重栽operator[],比如这样:
map<int **, string>
int *operator[](int index) |
把两个key拼接到一起,使用map不就可以了吗? |
map<string, map<string, KeyT> > |
没错,确实可以这么实现,下午的时候也想过,后来转不过弯来,模板套模板的我就糊涂了,实现了一个楼主看看:
[code]#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, int> map1;
map<string, map<string, int> > map2;
map1["test"] = 2;
map2["test1"] = map1;
cout << map2["test1"]["test"] << endl;
return 0;
}[/code] |
| |