基于此处的 boost 文档:
http://www.boost.org/doc/libs/1_41_0/doc/html/boost_propertytree/container.html
“一个节点中可能有多个具有相同键值的子节点。但是,这些子节点不一定是连续的。find返回的迭代器可能引用其中的任何一个,并且不能保证子节点的相对位置其他同名的 child 。”
示例 XML:
<library>
<book><title>Title_1</title></book>
<book><title>Title_2</title></book>
<book><title>Title_3</title></book>
</library>
示例增强代码:
ptree pt;
pt.push_back(ptree::value_type("book", ptree("title")))
// This finds the first book and cannot iterate to the second one:
ptree::const_iterator it = pt.find("book");
知道了这一点,您将如何获得所有书籍并确保您将它们全部看完?
最佳答案
您必须使用equal_range
函数:
std::pair < ptree::const_assoc_iterator, ptree::const_assoc_iterator> bounds =
pt.equal_range("book");
for (ptree::const_assoc_iterator it = bounds.first; it != bounds.second ; ++it)
{
// process *it
}
关于c++ - 增强属性树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327032/