提升属性树:使用指向节点及其父节点的指针删除节点
Boost property tree: Remove a node using pointers to the node and its parent node
我想从基于 xml 的 ptree 中删除一个节点:
<library>
<booklist>
<book id="10">
<data title="t1"/>
</book>
<book id="10">
<data title="t2"/>
</book>
<book id="10">
<data title="t3"/>
</book>
<book id="20">
<data title="t4"/>
</book>
</booklist>
</library>
我有一种算法可以找到正确的节点,该节点返回指向删除节点的指针。我还有一个指向删除节点父节点的指针。但是 erase() 将采用迭代器(而不是指针)。我的问题是如何使用两个指针删除节点;一个指向移除节点的 poiter,另一个指向父节点。
void removeElement(const std::string addr, const std::string criteria, boost::property_tree::ptree &ptSource)
{
boost::property_tree::ptree *ptParent = findParentPTree(addr, criteria, ptSource); // Points to "library.booklist"
boost::property_tree::ptree *ptRemove = findRemovePTree(addr, criteria, ptSource); // eg the third <book> which contains the <data title="t3"/>
// question: how to remove node ptRemove from ptSource?
}
请注意,有一些使用迭代器的示例,但不清楚应该如何找到删除节点的迭代器。
实际上,没有直接函数可以从值引用中获取迭代器。所以你必须自己写:
- C++:提升 ptree 相对键
在这种情况下,您似乎不需要它是递归的,因此更简单:
住在科里鲁
#include <iostream>
#include <boost/property_tree/ptree.hpp>
using namespace boost::property_tree;
ptree::iterator child_iterator(ptree& within, ptree const& child) {
for (auto it = within.begin(); it != within.end(); ++it)
if (std::addressof(it->second) == std::addressof(child))
return it;
return within.end();
}
ptree* findParentPTree(std::string const, std::string const&, ptree const&);
ptree* findRemovePTree(std::string const, std::string const&, ptree const&);
void removeElement(const std::string& addr, const std::string& criteria, ptree &ptSource)
{
ptree *ptParent = findParentPTree(addr, criteria, ptSource); // Points to "library.booklist"
ptree *ptRemove = findRemovePTree(addr, criteria, ptSource); // eg the third <book> which contains the <data title="t3"/>
auto it = child_iterator(*ptParent, *ptRemove);
if (it != ptParent->end())
ptParent->erase(it);
}
相关文章:
- 创建附加到每个节点的节点列表 (c++)
- 为什么我的节点临时节点不能显示正确的数据?
- 父节点中的垃圾值
- 查找二叉树的父节点函数
- 从父节点中删除子节点 - PugiXML
- 将子节点分配给父节点
- 简单C++指针:如何将子节点链接到父节点
- 如何在c++中遍历treeview控件的所有父节点
- 如何在左子右兄弟树中找到节点的父节点
- 如何创建一个带有指向父节点的指针的节点类,该父节点可以设置为空
- 在c++中,子节点适合父节点的位置
- 试图创建一个指向父节点的指针,同时构建二叉树
- 从指针到成员恢复父节点
- 获取父节点的类型
- 获取二叉搜索树的父节点
- Fork()使用c++创建父节点的4个子节点
- 父节点如何用一个管道向n个子节点发送消息?
- 在二叉搜索树中找到一个节点的父节点
- 没有从父节点接收moveEvent
- 使用pugixml将节点分配给新的父节点