升压1.46.1,物业树:如何通过ptree中接收子ptrees循环? [英] Boost 1.46.1, Property Tree: How to iterate through ptree receiving sub ptrees?

查看:122
本文介绍了升压1.46.1,物业树:如何通过ptree中接收子ptrees循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我要说,我认为我得到了它应该怎么做,但我的code将无法编译任何方式我试试。我根据我的假设空ptree中招的这个官方的例子。在那里,你可以找到下一行:

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:

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

其中表明,它是(或者应当是)有可能获得从ptree中subptree出

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

所以,我认为,我们可以遍历直通ptree中以这样的方式类似 BOOST_FOREACH

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

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

}

但我得到下一个错误:

But I get next error:

错误1错误C2440:初始化:不能从转换'的std ::对&LT; _Ty1,_Ty2>'到'常量的boost :: property_tree :: ptree中和放大器;

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

或者,如果我尝试

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

}

我得到:

错误1错误C2039:'empty_ptree':是不是'的boost :: property_tree

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

所以,我该怎么办?如何遍历直通升压ptree中,让子Ptrees

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

更新:
我也试过这样的code

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;
    }
}

这编译,不抛出任何产生的异常,但不清点任何子数据,它只是skeeps通过这个循环。

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

更新2:

Update 2:

唔...可能有问题在我的XML - 现在我得到与code正确的结果。

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

推荐答案

属性树迭代器指向对形式的(键,树)类型 ptree中:: VALUE_TYPE 。标准循环在路径所以看起来像通过节点的孩子迭代:

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 path therefore looks like:

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.
}

这篇关于升压1.46.1,物业树:如何通过ptree中接收子ptrees循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆