C++ boost : Simple nested dictionary?

标签 c++ boost dictionary nested

使用字符串键和字符串/整数/字典值实现嵌套字典的最简单方法是什么?

到目前为止我尝试过的很奇怪:
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

关于C++ boost : Simple nested dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265142/

相关文章:

c++ - 派生类对象是否包含基类的私有(private)成员?它在内存中是什么样子的?

c++ - 英特尔 SIMD - 如何检查 __m256* 是否包含任何非零值

c++ - 使用带有参数的 boost::function 来共享指向派生类的指针

c++ - 如何从另一张 map 构建一张 map ?

python - 如何将文本文件的每一行转换为字典条目?

c++ - 如何从列表中删除具有相应优先级队列的元素?

c++ - R* 树重叠计算

python - Boost.Python 快速入门链接器错误

c++ - boost 函数签名

python - 如果字典列表不包含特定值,请检查 python