c++中map的简写语法

shorthand syntax for c++ map in map

本文关键字:语法 map c++      更新时间:2023-10-16

如果我有如下定义:

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

我能不能概括一下,比如,

Foo_map<10>

并有一个10倍嵌套映射。我不需要boost::recursive_wrapper这样的东西因为层数总是恒定的

即使对于有限的c++元编程能力来说,这似乎也很容易:

#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;
}

这对我很有用。

#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Foo
{
   Foo() : _in(0) {}
   Foo(int in) : _in(in) {}
   int _in;
};
template <int N> struct Foo_map
{
   map<string, Foo_map<N-1> > foo_Map;
   Foo_map<N-1>& operator[](string const& key) { return foo_Map[key]; }
};
template <> struct Foo_map<1>
{
   map<string, Foo> foo_Map;
   Foo& operator[](string const& key) { return foo_Map[key]; }
};
int main()
{
   Foo_map<1> map1;
   map1["abcd"] = Foo(10);
   Foo_map<2> map2;
   map2["a"]["b"] = Foo(20);
   Foo_map<10> map10;
   map10["a"]["b"]["c"]["d"]["e"]["f"]["g"]["h"]["i"]["j"] = Foo(100);
   std::cout << map1["abcd"]._in << std::endl;
   std::cout << map2["a"]["b"]._in << std::endl;
   std::cout << map10["a"]["b"]["c"]["d"]["e"]["f"]["g"]["h"]["i"]["j"]._in << std::endl;
}

运行程序的输出:

<>之前1020.One hundred.