如何检查属性树中是否存在路径?
示例:
boost::property_tree::ptree tree;
// if path doesn't exist, put value
if (/*...*/)
{
tree.put("my.path.to.thing", true);
}
答案 0 :(得分:2)
对于简单的解决方案,您可以使用get_optional()
根据文档,如果存在,则返回值,否则返回未初始化的optional
实施例
boost::property_tree::ptree tree;
// if path doesn't exist, put value
if (!tree.get_optional<bool>("my.path.to.thing").is_initialized())
{
tree.put("my.path.to.thing", true);
}