获取 boost property_tree 父节点

Ego*_*rov 3 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>
Run Code Online (Sandbox Code Playgroud)

结果按预期进行:

{0}: 1
    {0}: 2
        {0}: 1
    {1}: 2
        {0}: 2
        {1}: 1
Run Code Online (Sandbox Code Playgroud)

我需要的是一种在不永久存储节点的情况下获取节点完整 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. 父 == NULL => 结束

seh*_*ehe 5

你不能。

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

作为最佳近似,您可以在父级内部查找子级,例如在C++ 中使用类似的东西: boost ptree relative key

这假设您总是有“根”可用于搜索。