C 提升:简单的嵌套字典

C++ Boost: Simple nested dictionary?

本文关键字:嵌套 字典 简单 提升      更新时间:2023-10-16

用字符串键和字符串/int/dictionary值实现嵌套词典的最简单方法是什么?

我到目前为止尝试的是一个奇怪的:
boost::fusion::map<std::string, boost:variant<std::string, int, boost::fusion::map<std::string, boost:variant<std::string, int>>>>

这看起来像是深度2,而且看起来绝对无法正常工作。即使愿意,我也对如何使用它感到困惑。

有什么想法?

您可以这样定义它,

template<int N> struct my_map {
    typedef boost::variant<int, std::string, typename my_map<N-1>::type> value_type;
    typedef std::map<std::string, value_type> type;
};
template<> struct my_map<1> {
    typedef boost::variant<int, std::string> value_type;
    typedef std::map<std::string, value_type> type;
};

可能用boost::fusion::map替换std::map,但是也许您会以更简单的设计使用(智能)指针将其链接到nullptr时,当您需要终止时。