检查提升属性树中是否存在路径

Checking if a path exists in a boost property tree

本文关键字:是否 存在 路径 属性 检查      更新时间:2023-10-16

如何检查属性树中是否存在路径?

例:

boost::property_tree::ptree tree;
// if path doesn't exist, put value
if (/*...*/)
{
    tree.put("my.path.to.thing", true);
}

对于一个简单的解决方案,您可以使用 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);
}