yaml-cpp read sequence in item

后端 未结 1 2074
名媛妹妹
名媛妹妹 2020-12-18 10:55

How can I read this YAML file with yaml-cpp:

sensors:
  - id: 5
    hardwareId: 28-000005a32133
    type: 1
  - id: 6
    hardwareId: 28-000005a32132
    typ         


        
相关标签:
1条回答
  • 2020-12-18 11:28

    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";
    }
    
    0 讨论(0)
提交回复
热议问题