C++ Yaml文件解析安装及使用
安装 yaml-cpp
克隆官方库
git clone https://github.com/jbeder/yaml-cpp.git
编译 yaml-cpp
cd yaml-cpp # 进入克隆的文件夹
mkdir build
cd build
cmake ..
make
make install
没出现报错的话就完成了编译
示例代码
robot.cpp
#include "yaml-cpp/yaml.h" //安装yaml-cpp参考google code 主页
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
// our data types //这个例子好像是取自开源游戏引擎ogre,随便说的
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)