#include <boost/property_tree/ptree.hpp>
#include <string>
#include <iostream>
int main()
{
boost::property_tree::ptree ptree;
const std::string entry = "server.url";
ptree.add( entry, "foo.com" );
auto range = ptree.equal_range( entry );
for( auto iter = range.first ; iter != range.second ; ++iter )
std::cout << iter->first << 'n';
}
我不明白为什么这段代码没有打印。由于可能有很多 server.url 条目,我尝试使用 equal_range
访问它们。
>equal_range
不适用于路径。添加后,您的 ptree 如下所示:
<root>
"server"
"url": "foo.com"
但是equal_range
直接在根节点中查找名为"server.url"的子节点。
另外,您可能希望打印出it->second.data()
,因为前者只会为每个找到的条目打印"server.url"。
以下是更正后的代码:
#include <boost/property_tree/ptree.hpp>
#include <string>
#include <iostream>
int main()
{
boost::property_tree::ptree ptree;
const std::string entry = "server.url";
ptree.add( entry, "foo.com" );
auto range = ptree.get_child("server").equal_range( "url" );
for( auto iter = range.first ; iter != range.second ; ++iter )
std::cout << iter->second.data() << 'n';
}