Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 项目中的yaml cpp读取顺序_C++_Yaml_Yaml Cpp - Fatal编程技术网

C++ 项目中的yaml cpp读取顺序

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);

如何使用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);
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;istd::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::迭代器