gpt4 book ai didi

c++ - Boost::ini_parser:读取特定部分值的方法是什么?

转载 作者:行者123 更新时间:2023-11-28 06:11:37 26 4
gpt4 key购买 nike

我正在使用 boost::property_tree 来读取 .ini 文件。

我知道我可以阅读特定的 key(inside section) -> iniTree.get<std::string>("section.key") .

我知道我可以读取 ini 文件中的所有值。

我只想读取特定部分的键。

类似的东西:iniTree.get<std::vector<std::string> >("section") .

这可能吗?

最佳答案

是的。您使用“get_child”获取子树。

如果您事先不知道该部分是否存在,您可以使用 get_child_optional

这是一个显示两种变体的演示:

Live On Coliru

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <fstream>
#include <iostream>

using boost::property_tree::ptree;

int main() {

std::fstream ifs("input.txt");
ptree pt;

read_ini(ifs, pt);

// let's get just the section2

if (boost::optional<ptree&> oops = pt.get_child_optional("oops")) {
std::cout << "There is a section `oops`\n";
} else {
std::cout << "There is NO section `oops`\n";
}

ptree& sub1 = pt.get_child("section2"); // we want the CCs!
write_ini(std::cout, sub1);
}

给定一个 input.txt 为:

[section1]

huh=value1
slam=value2
cram=value3

[section2]

rabbits=creditcard1
die=creditcard2
eagerly=creditcard3

它将打印输出:

There is NO section `oops`
rabbits=creditcard1
die=creditcard2
eagerly=creditcard3

关于c++ - Boost::ini_parser:读取特定部分值的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com