【问题标题】:How to parse a file with yaml-cpp如何使用 yaml-cpp 解析文件
【发布时间】:2019-06-23 16:29:48
【问题描述】:

我有一个如下所示的 yaml 文件:

construction_cone_1:
  model: construction_cone
  model_type: sdf
  position: [ 1.2, 3.4, 0.0 ]
  orientation: [ 0.0, 0.0, 0 ]
  
construction_cone_2:
  model: construction_cone
  model_type: sdf
  position: [ 3.0, 7.0, 0.0 ]
  orientation: [ 0.0, 0.0, 0 ]
 
...

我正在按照 this 教程在我的 c++ 应用程序中解析它。

到目前为止,我的理解是,由于它的结构,该文件作为映射加载到 YAML::Node 中。所以,我想,阅读它的一个好方法是:

YAML::Node map = YAML::LoadFile(file_path);
  for(YAML::const_iterator it=map.begin(); it!=map.end(); ++it){
    const std::string &key=it->first.as<std::string>();

这给了我第一个条目的“construction_cone_1”,依此类推。通过遵循这个逻辑,我无法弄清楚如何阅读其余部分。特别是,对于地图的每个条目,我都对读取对象位置感兴趣。

【问题讨论】:

    标签: c++ parsing dictionary yaml yaml-cpp


    【解决方案1】:

    我想我低估了图书馆的力量。事实证明,这样做可以解决问题:

      YAML::Node map = YAML::LoadFile(filename);
      for(YAML::const_iterator it=map.begin(); it!=map.end(); ++it){
        const std::string &key=it->first.as<std::string>();
    
        Eigen::Vector2f pos;
        YAML::Node attributes = it->second;
        YAML::Node position = attributes["position"];
        for(int i=0; i<2; ++i){
          pos(i) = position[i].as<float>();
        }
    
        ...
      }
    

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 1970-01-01
      • 2018-10-13
      • 2015-12-06
      • 1970-01-01
      • 2015-05-14
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多