#include <yaml-cpp/yaml.h>
#include <string>
using namespace std;
int main() {
std::string file_path;
// 读取yaml文件
YAML::Node yaml_node = YAML::LoadFile(file_path);
// 读取yaml字符串
std::string yaml_content;
yaml_node = YAML::Load(yaml_content);
// 读取文件中,a对应的内容
YAML::Node yaml_node_a = yaml_node["a"];
// 判断文件中是否存在a这个key
if (yaml_node_a != nullptr) {
// exist
} else {
// not exist
}
// 读取文件中,a中的b对应的内容
YAML::Node yaml_node_a_b = yaml_node["a"]["b"];
// 假设a是string的解析方式
string str_a = yaml_node_a.as<string>();
// 假设a是int的解析方式
int int_a = yaml_node_a.as<int>();
// 判断a是否是一个map
bool is_map = yaml_node_a.IsMap();
// 假设a是map时的遍历方式
for (auto it = yaml_node_a.begin(); it != yaml_node_a.end(); ++it) {
// 获取key
std::string key = it->first.as<string>();
// 获取value,假设value还是YAML::Node
YAML::Node value = it->second;
// 对value进一步处理
}
// 判断a是否是一个数组
bool is_seq = yaml_node_a.IsSequence();
// a中元素的个数
int size = yaml_node_a.size();
// 假设a是数组时的遍历方式
for (int i = 0; i < size; ++i) {
// 假设数组内部是string
std::string value = yaml_node_a[i].as<string>();
}
return 0;
}
原文链接: https://www.cnblogs.com/umichan0621/p/16533241.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/190593
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!