是否有一种方便的方法可以从属性树中删除节点,并保留其子节点? [英] Is there a convenient way to erase a node from a property tree, preserving its child nodes?

查看:115
本文介绍了是否有一种方便的方法可以从属性树中删除节点,并保留其子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从boost属性树中删除一个节点,但是我想保留其子节点并将其连接到已删除节点的父节点(即其祖父母节点)。

I want to delete a node from a boost property tree, but I want to preserve its children and connect them to the parent of the deleted node (i.e. to their grandparent node). Is there an elegant way to achieve this?

推荐答案

这可能是移动孙辈的最有效方法:

This might be the most efficient way to move the grandchildren:

std::move(middle.begin(), middle.end(), back_inserter(parent));

完整样本

< kbd> 在Coliru上直播

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;

int main() {

    std::istringstream iss(R"({ "a" : { "middle" : { "a1":1, "a2":2, "a3":3 }, "more":"stuff" } })");
    ptree pt;
    read_json(iss, pt);

    auto& parent = pt.get_child("a");
    auto& middle = pt.get_child("a.middle");

    std::move(middle.begin(), middle.end(), back_inserter(parent));
    parent.erase("middle");

    write_json(std::cout, pt);

}

示例json输出:

{
    "a": {
        "more": "stuff",
        "a1": "1",
        "a2": "2",
        "a3": "3"
    }
}

这篇关于是否有一种方便的方法可以从属性树中删除节点,并保留其子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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