如何在 c++ 中展平字典
How do I flatten a dictionary in c++
如何展平下面的字典 -
输入:
dict = {
"Key1" : "1",
"Key2" : {
"a" : "2",
"b" : "3",
"c" : {
"d" : "3",
"e" : {
"" : "1"
}
}
}
}
输出:
{
"Key1" : "1",
"Key2.a" : "2",
"Key2.b" : "3",
"Key2.c.d" : "3",
"Key2.c.e" : "1"
}
输入映射的数据类型为map<string, void*>
预期的输出映射为map<string,string>
使用 boost PropertyTree。PropertyTree能够表示分层结构,甚至带有一个小的json解析器。它还具有符合 std 的迭代器,因此您可以迭代树并将每个节点复制到输出映射中,从而有效地展平树。
像这样的东西(取自 https://www.boost.org/doc/libs/1_65_1/doc/html/property_tree/tutorial.html(:
namespace pt = boost::property_tree;
ptree tree;
// read the hierarchical data into the tree
pt::read_json(filename, tree);
map<string, string> out;
flatten(tree, out);
其中flatten
递归迭代树:
void flatten(const pt::tree &tree, map<string, string> &out)
{
for (const auto &j:tree)
{
copy_to_out(j, out);
if (is_tree(j))
flatten(j, out);
}
}
相关文章:
- C++中的MSD基排序(字典序)
- C++中是否有任何函数等同于python中字典的get函数?
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- C++中类似Python字典的结构?
- C++ 按数值对元组<字符串、浮点数>然后按字典顺序排序的向量
- 使用C++对一个巨大文件中的文本行进行字典式外部排序
- 与C++std::map::insert行为相比,C#排序字典的效率
- 为字典cpp创建[]=运算符
- C++ 随机生成的字典单词将仅显示以 B 开头的单词
- 如何将字典添加到向量?
- 蟒蛇字典到C++映射
- 使用给定的字典构建C++翻译器?
- C++类:如何使用字典["hello"]=20;像蟒蛇
- 如何在 c++ 中展平字典
- 在 c++ 中访问嵌套字典unordered_map<字符串、void*>?
- 如何将返回的 Python 字典转换为 C++ std::map<string,string>
- 拉丁语的二进制字典 - 如何构建或在哪里找到
- STL 中字典的最佳容器
- 调用C 字典的功能图
- 编写链接函数的更短/更有效的方法,该函数按字典顺序添加新元素