我正在使用 Boost.PropertyTree
读取 INI 文件:
read_ini( "myIni.ini", pt );
string s=pt.get<std::string>("Section1.value1");
如果部分不包含 value1
记录,那么 Boost 会引发异常。
如果 Section1.value1
不存在,如何以优雅的方式读取 INI 文件并为 s
赋予默认值?
最佳答案
使用 Boost.Optional
:
s = pt.get_optional<std::string>("Section1.value1").get_value_or("default");
// ^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^
关于c++ - 当值部分不存在时使用 Boost Property Tree 读取 INI 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25760285/