C++ 如何制作可变深度的嵌套贴图

C++ 如何制作可变深度的嵌套贴图,c++,map,nested,stdmap,C++,Map,Nested,Stdmap,我希望能够使用另一个std::map的值创建一个std::map,并希望能够将此映射嵌套到任意深度 以下是一个基本示例: std::map < std::string, std::map < std::string, int> > d1; // so the next depth would be. std::map < std::string, std::map < std::string, std::map < std::string

我希望能够使用另一个std::map的值创建一个std::map,并希望能够将此映射嵌套到任意深度

以下是一个基本示例:

std::map < std::string, std::map < std::string, int> > d1;

//       so the next depth would be.

std::map < std::string, std::map < std::string, std::map < std::string, int> > > d2;
std::map>d1;
//所以下一个深度是。
std::map>d2;

我知道这对于固定长度来说很简单,但我不确定如何构建一个可变深度的建筑。

由于我们不能专门使用
类,我们必须使用
+
类型定义
,并最终使用
将其公开:

template<typename Key, typename Value, unsigned int N>
struct VarMapHelper
{
    typedef std::map<Key, typename VarMapHelper<Key, Value, N-1>::type> type;
};

template<typename Key, typename Value>
struct VarMapHelper<Key, Value, 1>
{
    typedef std::map<Key, Value> type;
};

template<typename Key, typename Value, unsigned int N>
using VarMap = typename VarMapHelper<Key, Value, N>::type;
模板
结构VarMapHelper
{
typedef std::映射类型;
};
模板
结构VarMapHelper
{
typedef std::映射类型;
};
模板
使用VarMap=typename VarMapHelper::type;

像这样使用它:

VarMap<std::string, int, 3> map;
VarMap;

如果将类误用为
VarMap
,为了防止编译器崩溃,我们还可以为0提供专门化:

template<typename Key, typename Value>
struct VarMapHelper<Key, Value, 0>
{
    static_assert(false, "Passing variable depth '0' to VarMap is illegal");
};
模板
结构VarMapHelper
{
static_assert(false,“将变量深度“0”传递给VarMap是非法的”);
};

您能否举例说明如何使用此类类?