yaml-cpp读取项目中的序列 [英] yaml-cpp read sequence in item
本文介绍了yaml-cpp读取项目中的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用yaml-cpp读取此YAML文件:
How can I read this YAML file with yaml-cpp:
sensors:
- id: 5
hardwareId: 28-000005a32133
type: 1
- id: 6
hardwareId: 28-000005a32132
type: 4
我不明白如何获得sensors
物品来使用它.
I can't understand how can I get sensors
item, to use it.
据我了解,sensors
是YAML::Node
.我怎么能得到它?
As I understand sensors
is a YAML::Node
. How can I get it?
更新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;
}
此代码有效,但它是使用有关旧api的教程编写的. 我认为可以使用迭代器重写此代码,但现在不知道如何.
This code works, but it was writed using tutorial about old api. I think this code could be rewrited with iterators, but I don't now how.
推荐答案
看起来您的代码有效,但是如果要使用迭代器重写它,则可以:
It looks like your code works, but if you want to rewrite it with iterators, you can:
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-cpp读取项目中的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文