【发布时间】: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
如何检查属性树中是否存在路径?
例子:
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
对于一个简单的解决方案,您可以使用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);
}
【讨论】: