C++ 如何返回最后一个boost ptree项目

C++ 如何返回最后一个boost ptree项目,c++,boost,boost-propertytree,C++,Boost,Boost Propertytree,我想创建一个函数,用于查找boost::property_tree::ptree中的最后一项并将其作为ptree返回 我有下面的代码可以找到最后一项,但是我不知道如何将其ptree作为返回值传递-这应该是可能的,但是我没有找到任何不首先复制ptree就返回ptree的示例 希望这样做的原因是能够处理最后一个项目的更新和新值 BOOST_FOREACH( boost::property_tree::ptree::value_type const&rowPair, pt.get_child(

我想创建一个函数,用于查找boost::property_tree::ptree中的最后一项并将其作为ptree返回

我有下面的代码可以找到最后一项,但是我不知道如何将其ptree作为返回值传递-这应该是可能的,但是我没有找到任何不首先复制ptree就返回ptree的示例

希望这样做的原因是能够处理最后一个项目的更新和新值

BOOST_FOREACH( boost::property_tree::ptree::value_type const&rowPair, pt.get_child( "" ) ) 
{
    cout << rowPair.first << ": " << endl;

    BOOST_REVERSE_FOREACH( boost::property_tree::ptree::value_type const& itemPair, rowPair.second ) 
    {
            std::cout << "\t" << itemPair.first << " ";

            BOOST_FOREACH( boost::property_tree::ptree::value_type const& node, itemPair.second ) 
            {
                    std::cout << node.second.get_value<std::string>() << " ";
            }

            std::cout << std::endl;
            //TODO: return itemPair
            break;  
    }


    std::cout << std::endl;
}
如果Items.item只有一个项,则此操作非常有效,但是如果Items.item中有多个“Items”和多个“item”,则它将在第一个Items.item中插入新条目

<xml>
<listOfItems>
<Items>
  <item>
  </item>
  <item> 
  ... inserts new sub pt here
  </item>
</Items>
<Items>
  <item>
  </item>
  <item> 
  ... Should insert new sub pt here
  </item>
</Items>
</listOfItems>

... 在此插入新的子pt
... 应在此处插入新的子pt
因此,为了得到我想要的东西,我必须以某种方式改进insert语句,或者创建一个函数,该函数可以为我提供正确的项,然后插入到该项中。我找到了一个解决方案:

<?xml version="1.0" encoding="utf-8"?>
<listOfItems>
<Items>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
</Items>
<Items>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
   insert here...
</Items>
</listOfItems>

在这里插入。。。

boost::property\u tree::ptree::value\u类型findLastItem(boost::property\u tree::ptree pt)
{
树,空树;
BOOST\u REVERSE\u FOREACH(ptree::value\u type&v2,pt.get\u child(“列表项”,“空树))
{
如果(v2.first==“项目”)
{ 
BOOST\u REVERSE\u FOREACH(ptree::value\u type&v3,v2.秒)
{
如果(v3.first==“项目”){

“复制它”是什么意思?当你简单地
返回你找到的物品时会发生什么?我假设这段代码应该在一个单独的函数中(否则它在返回物品时就没有多大意义了)?那么您是否将
pt
作为参数传递给函数?如果是这样,有什么可以阻止您只做,例如
返回itemPair
?是的,很明显它是在一个单独的函数中。我最初有这样一个命令:pt.insert(pt.get\u child(“Items.item”).end(),subpt.front());--这将更新Items.item,并在其上附加新的子项--但是,如果我有多个item条目,那么它将始终附加到Items列表中的第一个条目,因此需要查找并指向最后一个条目。同样,当我简单地返回找到的条目时,会出现编译器错误,因此当将boost ptree作为返回值处理请尝试创建一个可以显示给我们的,并包括实际错误(完整和未编辑,以及复制粘贴的文本)我承认我最初的问题是工资-我想我真的没有完全理解这个问题-可以使用解决方案,但是我希望使用一行程序,而不是使用多个BOOST语句
<?xml version="1.0" encoding="utf-8"?>
<listOfItems>
<Items>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
</Items>
<Items>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
  <Item> </Item>
   insert here...
</Items>
</listOfItems>
boost::property_tree::ptree::value_type findLastItem(boost::property_tree::ptree pt)
{
  ptree _empty_tree;
  BOOST_REVERSE_FOREACH(ptree::value_type &v2, pt.get_child("listOfItems", _empty_tree))
  {
   if(v2.first == "Items")
   { 
       BOOST_REVERSE_FOREACH(ptree::value_type &v3, v2.second)
       {
           if(v3.first == "Item"){
              cout << "- OK: Found Last Item " << endl;
              return v3; // then use add_child on this to append a new item 
           }
       }
   }
  }
}