c++ - 获取boost property_tree父节点

标签 c++ boost boost-propertytree

我在我的程序中使用了 boost property_tree。我已经设置了使用自定义路径类型的树。我正在寻找的是获取特定节点的父节点 ID。

这是一个例子:

MetaStorageTree tree;

typedef boost::property_tree::basic_ptree<Framework::CommonClientServer::InterfacePathChain_t, MetaStorageTreeNode*>
    MetaStorageTreeNode_t;
class MetaStorageTree : public MetaStorageTreeNode_t;

MetaStorageTreeNode* node = new MetaStorageTreeNode(1);
MetaStorageTreeNode* node1 = new MetaStorageTreeNode(2);
tree.put(InterfacePathChain_t{0}, node);
tree.put(InterfacePathChain_t{0, 0}, node1);
tree.put(InterfacePathChain_t{0, 1}, node1);
tree.put(InterfacePathChain_t{0, 0, 0}, node);
tree.put(InterfacePathChain_t{0, 1, 0}, node1);
tree.put(InterfacePathChain_t{0, 1, 1}, node);

//InterfacePathChain_t is basically a vector<int>

结果符合预期:

{0}: 1
    {0}: 2
        {0}: 1
    {1}: 2
        {0}: 2
        {1}: 1

我需要的是一种获取节点完整 ID 而无需永久存储它的方法。我在想的是一种简单地获取其父节点 ID 并将其推到路径前面等到顶层的方法。但是我似乎没能在property_tree 中找到方法来做到这一点。这可能吗?如果不是,是否还有其他方法可以计算这种情况下的完整路径?

例如对于路径为 {0, 1, 0} 的节点:

  1. id == 0 => 路径 = {0}
  2. parent != NULL => parent.id == 1 => path = {1, 0}
  3. parent != NULL => parent.id == 0 => path = {0, 1, 0}
  4. parent == NULL => end

最佳答案

你不能。

Boost Ptree 节点是自包含的,不知道任何包含的数据结构(它是单链表的“树”等价物)。

作为最佳近似,您可以在父项中查找子项,例如与 C++: boost ptree relative key 中的类似内容.

这假定您始终有“根”可供搜索。

关于c++ - 获取boost property_tree父节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45366768/

相关文章:

c++ - 在 C/c++ 中实现 __index 元函数

c++ - 在内存中实现无限 map

c++ - 使用 boost::random 改组 vector 时出现问题

json - 从 JSON 数据提升访问 float 组

c++ - 如何从 Boost.PropertyTree 复制子树

c++ - 使用std::map应该是确定性的吗?

c++ bad_alloc,但它在 gdb 中消失了

c++ - Boost Property_Tree 迭代器,如何处理它们?

c++ - 如何正确检查 istreambuf_iterator 是否已到达流末尾

c++ - boost 计时器立即到期