YAML开源库yaml-cpp简介及使用

更新时间:2023-05-16 12:33:02 阅读: 评论:0

YAML开源库yaml-cpp简介及使⽤解析和产⽣yaml⽂件的测试代码如下:
#include "funt.hpp"
#include <string>
#include <fstream>
#include <yaml-cpp/yaml.h>
int test_par_yaml_file()
{
#ifdef _MSC_VER
YAML::Node config = YAML::LoadFile("E:/GitCode/Messy_Test/testdata/l");uniforms
#el
YAML::Node config = YAML::LoadFile("testdata/l");
#endif
std::string blog_name{"xxxxx"}, blog_id{"xxxxx"}, blog_url{"xxxxx"};
婊子英文if (config["blog"]) {
if (config["blog"]["name"])
blog_name = config["blog"]["name"].as<std::string>();
if (config["blog"]["id"])
blog_id = config["blog"]["id"].as<std::string>();
if (config["blog"]["url"])
blog_url = config["blog"]["url"].as<std::string>();
} el {
fprintf(stderr, "the node blog doesn't exist\n");
}
fprintf(stdout, "blog name: %s, id: %s, url: %s\n",
blog_name.c_str(), blog_id.c_str(), blog_url.c_str());
bool value1, value2;
if (config["value1"])
value1 = config["value1"].as<bool>();
if (config["value2"])
value2 = config["value2"].as<bool>();
fprintf(stdout, "value1: %d, value2: %d\n", value1, value2);
int number1;
std::string number2, number3;
float number4;
if (config["number1"])
number1 = config["number1"].as<int>();
if (config["number2"])
number2 = config["number2"].as<std::string>();
if (config["number3"])
number3 = config["number3"].as<std::string>();
if (config["number4"])
number4 = config["number4"].as<float>();
fprintf(stdout, "number1: %d, number2: %s, number3: %s, number4: %f\n",
number1, number2.c_str(), number3.c_str(), number4);
std::string github_url, github_repos;
if (config["github"])
github_url = config["github"][0].as<std::string>();
github_repos = config["github"][1].as<std::string>();
fprintf(stdout, "github url: %s, repos: %s\n", github_url.c_str(), github_repos.c_str());dettol
return 0;
}
int test_generate_yaml_file()
{
YAML::Node node;
node["language"] = "cpp";
node["version"] = 2;fork是什么意思
node["url"].push_back("/fengbingchun");what about now
node["url"].push_back("/fengbingchun");
YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
primes.push_back(13);
fprintf(stdout, "primes size: %d\n", primes.size());
node["primes"] = primes;
YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}"); lineup["RF"] = "Corey Hart";
lineup["C"] = "Jonathan Lucroy";
node["name"] = lineup;
node["platform"]["linux"].push_back("x86");款式的英文
node["platform"]["linux"].push_back("x86_64");
node["platform"]["linux"].push_back("armv7");
node["platform"]["windows"].push_back("x86");
node["platform"]["windows"].push_back("x86_64");
#ifdef _MSC_VER
std::ofstream fout("E:/GitCode/Messy_Test/l");
#el
std::ofstream fout("testdata/tmp.yaml");
#endif
body shopfout << node;
return 0;
}
l内容如下:
# config file
---
blog:
name: csdn
id: fengbingchun
url: /fengbingchun
commands:
version:
- g++ --version
-
cmake --version
- git --version
value1: true
value2: fal
value3: ~
number1: 123
number2: !!str 123
number3: "123"
number4: !!float 123
github:
- /fengbingchun
-
NN_Test Face_Test OpenCV_Test
Messy_Test CUDA_Test
data1: |
照耀的意思
There once
was a
accudshort man
data2: >
There once
was a
short man
date1: 2019-03-03
step: &id001  # defines anchor label &id001
instrument:  Lasik 2000
pulEnergy: 5.4
step2: *id001  # refers to the first step (with anchor &id001) ...
解析yaml⽂件输出结果如下:
产⽣yaml⽂件的内容如下:
raid

本文发布于:2023-05-16 12:33:02,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/110626.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:输出   解析   照耀   意思   结果   简介
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图