C++ 项目中的yaml cpp读取顺序
如何使用YAML cpp读取此YAML文件:C++ 项目中的yaml cpp读取顺序,c++,yaml,yaml-cpp,C++,Yaml,Yaml Cpp,如何使用YAML cpp读取此YAML文件: sensors: - id: 5 hardwareId: 28-000005a32133 type: 1 - id: 6 hardwareId: 28-000005a32132 type: 4 我不明白我怎样才能得到传感器项目来使用它 据我所知,sensors是一个YAML::Node。我怎样才能得到它 更新1: YAML::Node config = YAML::LoadFile(config_path);
sensors:
- id: 5
hardwareId: 28-000005a32133
type: 1
- id: 6
hardwareId: 28-000005a32132
type: 4
我不明白我怎样才能得到传感器
项目来使用它
据我所知,sensors
是一个YAML::Node
。我怎样才能得到它
更新1:
YAML::Node config = YAML::LoadFile(config_path);
const YAML::Node& node_test1 = confg["sensors"];
for (std::size_t i = 0; i < node_test1.size(); i++) {
const YAML::Node& node_test2 = node_test1[i];
std::cout << "Id: " << node_test2["id"].as<std::string>() << std::endl;
std::cout << "hardwareId: " << node_test2["hardwareId"].as<std::string>() << std::endl << std::endl;
}
YAML::Node config=YAML::LoadFile(配置路径);
const YAML::Node&Node_test1=confg[“传感器”];
对于(std::size_t i=0;i std::cout看起来您的代码可以工作,但是如果您想用迭代器重写它,您可以:
YAML::Node config = YAML::LoadFile(config_path);
const YAML::Node& sensors = config["sensors"];
for (YAML::iterator it = sensors.begin(); it != sensors.end(); ++it) {
const YAML::Node& sensor = *it;
std::cout << "Id: " << sensor["id"].as<std::string>() << "\n";
std::cout << "hardwareId: " << sensor["hardwareId"].as<std::string>() << "\n\n";
}
YAML::Node config=YAML::LoadFile(配置路径);
const YAML::Node&sensors=config[“sensors”];
for(YAML::iterator it=sensors.begin();it!=sensors.end();++it){
常量YAML::节点和传感器=*it;
std::cout你试过什么?阅读教程:看看这是否有帮助:)我已经阅读了这篇教程,我找到了如何读取序列,但我没有找到如何读取项目中的序列。正如我在示例中理解的那样,传感器也是节点。但我没有找到如何使用它作为YAML::node
我发现我需要使用YAML::const_迭代器
而不是YAML::迭代器