我在我的程序中使用了 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} 的节点:
- id == 0 => 路径 = {0}
- parent != NULL => parent.id == 1 => path = {1, 0}
- parent != NULL => parent.id == 0 => path = {0, 1, 0}
- parent == NULL => end
最佳答案
你不能。
Boost Ptree 节点是自包含的,不知道任何包含的数据结构(它是单链表的“树”等价物)。
作为最佳近似,您可以在父项中查找子项,例如与 C++: boost ptree relative key 中的类似内容.
这假定您始终有“根”可供搜索。
关于c++ - 获取boost property_tree父节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45366768/