[C.C++] C++ Yaml文件解析安装及使用 yaml-cpp

349 0
Honkers 2026-4-3 08:48:38 来自手机 | 显示全部楼层 |阅读模式

安装 yaml-cpp

克隆官方库

  1. git clone https://github.com/jbeder/yaml-cpp.git
复制代码

编译 yaml-cpp

  1. cd yaml-cpp # 进入克隆的文件夹
  2. mkdir build
  3. cd build
  4. cmake ..
  5. make
  6. make install
复制代码

没出现报错的话就完成了编译

示例代码

robot.cpp

  1. #include "yaml-cpp/yaml.h" //安装yaml-cpp参考google code 主页
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. // our data types //这个例子好像是取自开源游戏引擎ogre,随便说的
  7. struct Vec3 {
  8. //位置坐标
  9. float x, y, z;
  10. };
  11. struct Power {
  12. //招式,魔法
  13. std::string name; //招式名字,如 葵花宝典
  14. int damage; //伤害值
  15. };
  16. struct Monster {
  17. //怪兽
  18. std::string name;
  19. Vec3 position;
  20. std::vector <Power> powers;
  21. };
  22. // now the extraction operators for these types //重载 >> 预算符。。。。
  23. void operator >> (const YAML::Node& node, Vec3& v) {
  24. node[0] >> v.x;
  25. node[1] >> v.y;
  26. node[2] >> v.z;
  27. }
  28. void operator >> (const YAML::Node& node, Power
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行