【C++配置yaml】yaml-cpp使用

yaml-cpp

》》参考资料1
》》参考资料2

下载安装

安装yaml-cpp

git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir build && cd build
cmake .. && make -j
sudo make install

CMakeLists配置

find_package(yaml-cpp REQUIRED)
include_directories(${YAML_CPP_INCLUDE_DIR})
# add_executable (main ${COMMON}) #连接编译编译路径
# 动态连接yaml库
target_link_libraries(main yaml-cpp::yaml-cpp)

yaml-cpp使用

Node

Node是yaml-cpp中最重要的数据结构,Node一共有以下几种type

  1. Null 空节点
  2. Sequence 序列,类似于一个Vector,对应YAML格式中的数组
  3. Map 类似标准库中的Map,对应YAML格式中的对象
  4. Scalar 标量,对应YAML格式中的常量

node的增改查删

#include <iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <assert.h>

int main()
{
    YAML::Node node1;
    assert(node1.IsNull());  //1. 初始化的节点是Null类型

    node1["key"] = "value"; //2. 当你给它赋值键值对,它转变为Map类型
    node1["number"] = 255;
    assert(node1.IsMap()); 
    node1["seq"].push_back("first element");//seq对应的是一个Sequence
    node1["seq"].push_back("second element");
    std::cout << node1 << std::endl;
    /*输出结果:
    key: value
    number: 255
    seq:
    - first element
    - second element
    */
    node1.remove("key"); //删除

    YAML::Node node2;  
    node2.push_back("first item");//3. 它是一个sequence类型
    node2.push_back("second_item");
    node2.push_back("third_item");
    std::vector<int> v = {1,3,5,7,9};//给node_2插入了一个Sequence
    node2.push_back(v);
    assert(node2.IsSequence());//当然,node_2仍然是一个Sequence
    for(auto it = node2.begin(); it != node2.end(); it++)
        std::cout << *(it) << std::endl;
    /*输出结果:
    first item
    second_item
    third_item
    - 1
    - 3
    - 5
    - 7
    - 9
    */
   node2.remove(0);//删除first item
   node2[0] = "new fir";
    /*修改后
    second_item
    new fir
    - 1
    - 3
    - 5
    - 7
    - 9
    */
}

Node 相关API

  • .IsDefined():判断是否存在。
  • .as<type>():获取对应值。
  • .Type():获取对应的类型{ Undefined, Null, Scalar, Sequence, Map };(0,1,2,3,4)

yaml文件解析

用 loadFile() 可从文件生成Node

Node LoadFile(const std::string& filename)
//filename 就是yaml文件的路径

实战:

  • test.yaml
name: xiaoming
sex: male
age: 18
system:
  port: 0
  value: 0
  int_vec: [10, 20]
  • cpp文件
#include "log.h"
#include <iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <assert.h>

int main()
{
    YAML::Node node = YAML::LoadFile("./test.yaml");
    //获取类型
    std::cout << node.Type() << std::endl; //4
    std::cout << node["name"].Type() << std::endl;//2

    //获取内容
    std::cout << node["name"].as<std::string>() << std::endl;//xiaoming
    std::cout << node["sex"].as<std::string>() << std::endl;//male
    std::cout << node["age"].as<int>() << std::endl;//18
    std::cout << node["system"]["port"].as<std::string>() << std::endl;//8080
    std::cout << node["system"]["value"].as<std::string>() << std::endl;//0
    for(auto it = node["system"]["int_vec"].begin(); it != node["system"]["int_vec"].end(); it++)
    std::cout << *it << std::endl;//10 20

    //方式2
    for(auto it = node.begin(); it != node.end(); it++)//first指向key , second指向value
    std::cout << it->first << "  " << it->second << std::endl;
    /*读取结果
    name  xiaoming
    sex  male
    age  18
    system  port: 0
    value: 0
    int_vec: [10, 20]
    */

    //读取不存在的node值,报YAML::TypedBadConversion异常
    try{
        std::string label = node["label"].as<std::string>();
    }catch(YAML::TypedBadConversion<std::string> &e){
        std::cout<<"label node is NULL"<<std::endl;
    }

    //修改
    node["mg"] = 2;

    //保存config为yaml文件
    std::ofstream fout("./test.yaml"); 
    fout << node;
    fout.close();
    return 0;
}
  • 修改后test.yaml
name: xiaoming
sex: male
age: 18
system:
  port: 0
  value: 0
  int_vec: [10, 20]
mg: 2

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值