c++ 使用yaml-cpp解析YAML文件

5vf7fwbs  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(309)

我正在使用yaml-cpp 0.3.0(旧API版本)解析一个yaml文件(我从here获得)。
这是一个很好的例子。

- name: Ogre
  position: [0, 5, 0]
  powers:
    - name: Club
      damage: 10
    - name: Fist
      damage: 8
- name: Dragon
  position: [1, 0, 10]
  powers:
    - name: Fire Breath
      damage: 25
    - name: Claws
      damage: 15
- name: Wizard
  position: [5, -3, 0]
  powers:
    - name: Acid Rain
      damage: 50
    - name: Staff
      damage: 3

现在我试着用下面的代码解析异能名称和伤害:

#include "yaml-cpp/yaml.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// our data types
struct Vec3 {
   float x, y, z;
};

struct Power {
   std::string name;
   int damage;
};

struct Monster {
   std::string name;
   Vec3 position;
   std::vector <Power> powers;
};

// now the extraction operators for these types
void operator >> (const YAML::Node& node, Vec3& v) {
   node[0] >> v.x;
   node[1] >> v.y;
   node[2] >> v.z;
}

void operator >> (const YAML::Node& node, Power& power) {
   node["name"] >> power.name;
   node["damage"] >> power.damage;
}

void operator >> (const YAML::Node& node, Monster& monster) {
   node["name"] >> monster.name;
   node["position"] >> monster.position;
   const YAML::Node& powers = node["powers"];
   for(unsigned i=0;i<powers.size();i++) {
      Power power;
      powers[i] >> power;
      monster.powers.push_back(power);
   }
}

int main()
{
   std::ifstream fin("robot.yaml");
   YAML::Parser parser(fin);
   YAML::Node doc;
   parser.GetNextDocument(doc);
   for(unsigned i=0;i<doc.size();i++) {
      Power power;
      doc[i] ["powers"] >> power;
      std::cout << power.name << "\n";
      std::cout << power.damage << "\n";
   }

   return 0;
}

看起来我的代码显示了如下错误:

terminate called after throwing an instance of 'YAML::TypedKeyNotFound<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >'
  what():  yaml-cpp: error at line 4, column 5: key not found: name
Aborted (core dumped)

请帮助我理解为什么我不能解析powers元素。
并请向我解释我如何解析所有的键和值在一个代码,因为我是新的编码。

e5nszbig

e5nszbig1#

所以问题就在这里:

doc[i] ["powers"] >> power;

doc[i]["powers"]没有索引“名称”:

- name: Ogre   
  position: [0, 5, 0]   
  powers:
    - name: Club  
      damage: 10
    - name: Fist  
      damage: 8

“-”表示项目,因此类似于doc[i],首先需要索引:

Power power;
doc[i]["powers"][0] >> power;
std::cout << power.name //outputs "Club"

但是我假设你想要每一项的所有幂,所以你需要另一个显式的for循环:

for (unsigned i = 0; i < doc.size(); i++)
{
    for (unsigned j = 0; j < doc[i]["powers"].size(); ++j)
    {
        Power power;
        doc[i]["powers"][j] >> power;
        std::cout << power.name << "\n";
        std::cout << power.damage << "\n";
    }
}

但这有点过头了,最好直接使用Monster-class,因为功耗值已经存储在那里了:

for(unsigned i=0;i<doc.size();i++) {
    Monster monster;
    doc[i] >> monster;

    for (auto& power : monster.powers) // range-for loop
    {
        std::cout << power.name << "\n";
        std::cout << power.damage << "\n";
    }
    std::cout << monster.name << "\n";
}

也许这是一个好的开始。答案的延续将取决于你想要实现什么。

相关问题