获取提升property_tree父节点 [英] Getting boost property_tree parent node
问题描述
我在程序中使用了boost property_tree。我已经将树设置为使用自定义路径类型。我正在寻找的是获取特定节点的父节点ID。
I am using boost property_tree in my program. I've set up the tree for using custom path type. What I'm looking for is getting a specific node's parent node 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中找到一种方法来执行此操作。这可能吗?如果不是,是否有其他方法可以计算这种情况下的完整路径?
What I need is a way of getting full id of a node without permanently storing it. What I am thinking of is a way of simply getting its parent node id and pushing it to the front of the path and so on to the top level. But I didn't seem to be able to find a way to do this in property_tree. Is this possible? If no, are there any other ways of calculating full path for this case?
例如,路径为{0,1,0}的节点:
For example as for node with path {0, 1, 0}:
- id == 0 =>路径= {0}
- parent!= NULL => parent.id = = 1 =>路径= {1,0}
- 父母!= NULL => parent.id == 0 =>路径= {0,1,0}
- parent == NULL => end
- id == 0 => path = {0}
- parent != NULL => parent.id == 1 => path = {1, 0}
- parent != NULL => parent.id == 0 => path = {0, 1, 0}
- parent == NULL => end
推荐答案
。
Boost Ptree节点是独立的,不知道任何包含的数据结构(这是单链列表的树等效项)。
Boost Ptree nodes are self-contained and do not know about any containing datastructure (it's the the "tree" equivalent of a singly-linked list).
作为最佳近似,您可以在父母内部查找孩子与类似 C ++:boost ptree相对键。
As a best approximation you could look up the child inside the parent, e.g. with something like in C++: boost ptree relative key.
这假定您始终可以搜索 root。
This presumes you always have "a root" available to search in.
这篇关于获取提升property_tree父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!