【问题标题】:Checking if a path exists in a boost property tree检查提升属性树中是否存在路径
【发布时间】:2018-01-16 06:21:51
【问题描述】:

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

例子:

boost::property_tree::ptree tree;

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

【问题讨论】:

    标签: c++ json xml boost boost-propertytree


    【解决方案1】:

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

    【讨论】:

    • 如果 "my.path.to.thing" 存在但不是布尔值,则 tree.get_optional("my.path.to.thing").is_initialized() 返回 false跨度>
    • stackoverflow.com/questions/7568607/… 提供了更好的答案
    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多