C++ Boost 1.46.1,属性树:如何迭代接收子 ptree 的 ptree?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6656380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:29:13  来源:igfitidea点击:

Boost 1.46.1, Property Tree: How to iterate through ptree receiving sub ptrees?

c++boostsubtreeboost-propertytreeboost-foreach

提问by Rella

First of all I shall say that I think I got how it should be done but my code will not compile any way I try. I based my assumption on this official example of empty ptree trick. There you can find next line:

首先,我想说我想我知道应该怎么做,但我的代码不会以我尝试的任何方式编译。我的假设基于这个空 ptree 技巧的官方示例。在那里你可以找到下一行:

  const ptree &settings = pt.get_child("settings", empty_ptree<ptree>());

Which shows that it is (or should be) possible to get subptree out from ptree.

这表明可以(或应该)从 ptree 中取出 subptree。

So I assumed we could iterate thru ptree with something like BOOST_FOREACHin such manner:

所以我假设我们可以用BOOST_FOREACH这样的方式遍历 ptree :

BOOST_FOREACH(const boost::property_tree::ptree &v,
    config.get_child("servecies"))
{

}

But I get next error:

但我得到下一个错误:

Error 1 error C2440: 'initializing' : cannot convert from 'std::pair<_Ty1,_Ty2>' to 'const boost::property_tree::ptree &'

错误 1 ​​错误 C2440:“正在初始化”:无法从“std::pair<_Ty1,_Ty2>”转换为“const boost::property_tree::ptree &”

or if I try

或者如果我尝试

BOOST_FOREACH(boost::property_tree::ptree &v,
    config.get_child("servecies", boost::property_tree::empty_ptree<boost::property_tree::ptree>()))
{

}

I get:

我得到:

Error 1 error C2039: 'empty_ptree' : is not a member of 'boost::property_tree'

错误 1 ​​错误 C2039:“empty_ptree”:不是“boost::property_tree”的成员

So what shall I do: how to iterate thru Boost Ptree and get sub Ptrees?

那么我该怎么办:如何遍历 Boost Ptree 并获得子 Ptree?

Update:I also tried such code

更新:我也试过这样的代码

    BOOST_FOREACH(boost::property_tree::ptree::value_type &v,
    config.get_child("path.to.array_of_objects"))
{
    std::cout << "First data: " << v.first.data() << std::endl;
    boost::property_tree::ptree subtree = (boost::property_tree::ptree) v.second ;
    BOOST_FOREACH(boost::property_tree::ptree::value_type &vs,
        subtree)
    {
        std::cout << "Sub data: " << vs.first.data() << std::endl;
    }
}

This compiles, does not throw any exeptions but does not cout any Sub data, it just skeeps thru this cycle.

这会编译,不会抛出任何Sub data异常,但不会抛出任何异常,它只是通过这个循环。

Update 2:

更新 2:

Hm... something probably went wrong in my xml - now I get correct results with that code.

嗯...我的 xml 中可能出了点问题 - 现在我用该代码得到了正确的结果。

回答by antonakos

The property tree iterators point to pairs of the form (key, tree)of type ptree::value_type. The standard loop for iterating through the children of the node at paththerefore looks like:

属性树迭代器指向(key, tree)type形式的对ptree::value_type。因此,迭代节点子节点的标准循环path如下所示:

BOOST_FOREACH(const ptree::value_type &v, pt.get_child(path)) {
    // v.first is the name of the child.
    // v.second is the child tree.
}

回答by Ela782

Using C++11, you can use the following to iterate through all the children of the node at path:

使用 C++11,您可以使用以下命令遍历节点的所有子节点path

ptree children = pt.get_child(path);
for (const auto& kv : children) {
    // kv is of type ptree::value_type
    // kv.first is the name of the child
    // kv.second is the child tree
}

回答by Evalds Urtans

I had the same problem with iterating trough JSON sub nodes

我在遍历 JSON 子节点时遇到了同样的问题

boost::property_tree::read_json(streamJSON, ptJSON);

If you have a structure like:

如果你有这样的结构:

{
 playlists: [ {
   id: "1",
   x: "something"
   shows: [
    { val: "test" },
    { val: "test1" },
    { val: "test2" }
   ]
 },
 {
   id: "2"
   x: "else",
   shows: [
    { val: "test3" }
   ]
 }
 ]
}

You can iterate trough child nodes like this:

您可以像这样遍历子节点:

BOOST_FOREACH(boost::property_tree::ptree::value_type &playlist, ptJSON.get_child("playlists"))
{
    unsigned long uiPlaylistId = playlist.second.get<unsigned long>("id");
    BOOST_FOREACH(boost::property_tree::ptree::value_type &show, playlist.second.get_child("shows."))
    {
       std::string strVal = show.second.get<std::string>("val");
    }
}

I could not find anything about path selector "shows." to select sub array. (notice the dot at the end)

我找不到有关路径选择器“显示”的任何信息。选择子数组。(注意末尾的点)

Some good documentation can be found here: http://kaalus.atspace.com/ptree/doc/index.html

一些好的文档可以在这里找到:http: //kaalus.atspace.com/ptree/doc/index.html

Hope this helps someone.

希望这可以帮助某人。