c ++ map在地图中的速记语法 [英] shorthand syntax for c++ map in map
本文介绍了c ++ map在地图中的速记语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有如下定义:
typedef map<string, Foo> Foo_map_1
typedef map<string, Foo_map_1> Foo_map_2
typedef map<string, Foo_map_2> Foo_map_3
typedef map<string, Foo_map_3> Foo_map_4
typedef map<string, Foo_map_4> Foo_map_5
有没有我可以推广,所以我可以做,例如
Is there anyway I can generalize that so I could do, for example,
Foo_map<10>
并有一个10倍的嵌套映射。我不需要像 boost :: recursive_wrapper
这样的东西,因为关卡的数量总是不变的。
and have a 10-fold nested map. I don't need something like boost::recursive_wrapper
because the number of levels is always constant.
推荐答案
这似乎很容易,即使对有限的C ++元编程能力:
This is seems easy enough even for the limited C++ metaprogramming power:
#include <map>
#include <string>
template<int N, typename K, typename V>
struct NMap { typedef std::map<K, typename NMap<N-1, K, V>::type> type; };
template<typename K, typename V>
struct NMap<1, K, V> { typedef std::map<K, V> type; };
int main(int argc, const char *argv[]) {
NMap<3, int, std::string>::type m;
m[1][2][3] = "Test";
return 0;
}
这篇关于c ++ map在地图中的速记语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文