c++ 读写yaml配置文件

yizhihongxing

标题:C++读写YAML配置文件完整攻略

简介

YAML是一种人类可读的数据序列化格式,通常用于配置文件、数据交换、日志记录等。本文将介绍如何在C++中读写YAML配置文件的完整攻略。

依赖

  • yaml-cpp:一个C++的YAML解析库,用于读写YAML格式文件,可以在官网(https://github.com/jbeder/yaml-cpp)上下载。

基本使用方法

  1. 加载YAML文件
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) { 
    // 创建YAML文档对象
    YAML::Node config = YAML::LoadFile("config.yaml");

    // 输出整个文档流
    std::cout << "config:\n" << config << std::endl;

    // 访问文档中的值
    std::string name = config["name"].as<std::string>();
    int age = config["age"].as<int>();

    // 输出访问到的值
    std::cout << "name: " << name << ", age: " << age << std::endl;

    return 0;
}
  1. 存储YAML文件
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) {
    // 创建YAML文档对象
    YAML::Emitter out;
    out << YAML::BeginMap;
    out << YAML::Key << "name";
    out << YAML::Value << "Lucy";
    out << YAML::Key << "age";
    out << YAML::Value << 18;
    out << YAML::EndMap;

    // 将文档对象写入文件中
    std::ofstream fout("config.yaml");
    fout << out.c_str(); // 或者用fout << out.c_str() << std::endl;
    fout.close();

    return 0;
}

示例一:读写vector

  1. 读取vector
nums:
    - 1
    - 3
    - 5
    - 7
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>
#include <vector>

int main(int argc, char* argv[]) {
    // 加载YAML文件
    YAML::Node config = YAML::LoadFile("config.yaml");

    // 访问文档中的vector
    std::vector<int> nums = config["nums"].as<std::vector<int>>();

    // 输出访问到的vector
    std::cout << "nums: ";
    for(auto num : nums) {
        std::cout << num << " ";
    }

    return 0;
}

输出结果:

nums: 1 3 5 7
  1. 存储vector
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>
#include <vector>

int main(int argc, char* argv[]) {
    std::vector<int> nums = {1, 3, 5, 7};

    // 创建YAML文档对象
    YAML::Emitter out;
    out << YAML::BeginMap;
    out << YAML::Key << "nums";
    out << YAML::Value << YAML::BeginSeq;
    for(auto num : nums) {
        out << num;
    }
    out << YAML::EndSeq;
    out << YAML::EndMap;

    // 将文档对象写入文件中
    std::ofstream fout("config.yaml");
    fout << out.c_str();
    fout.close();

    return 0;
}

存储结果:

nums:
    - 1
    - 3
    - 5
    - 7

示例二:读写嵌套的map

  1. 读取嵌套map
person:
    name: John
    age: 22
    contact:
        email: john@example.com
        phone: 123456789
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>
#include <map>

int main(int argc, char* argv[]) {
    // 加载YAML文件
    YAML::Node config = YAML::LoadFile("config.yaml");

    // 访问文档中的值
    std::string name = config["person"]["name"].as<std::string>();
    int age = config["person"]["age"].as<int>();
    std::string email = config["person"]["contact"]["email"].as<std::string>();
    int phone = config["person"]["contact"]["phone"].as<int>();

    // 输出访问到的值
    std::cout << "name: " << name << ", age: " << age << std::endl;
    std::cout << "email: " << email << ", phone: " << phone << std::endl;

    return 0;
}

输出结果:

name: John, age: 22
email: john@example.com, phone: 123456789
  1. 存储嵌套map
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>
#include <map>

int main(int argc, char* argv[]) {
    // 创建YAML文档对象
    YAML::Emitter out;
    out << YAML::BeginMap;
    out << YAML::Key << "person";
    out << YAML::Value << YAML::BeginMap;
    out << YAML::Key << "name";
    out << YAML::Value << "John";
    out << YAML::Key << "age";
    out << YAML::Value << 22;
    out << YAML::Key << "contact";
    out << YAML::Value << YAML::BeginMap;
    out << YAML::Key << "email";
    out << YAML::Value << "john@example.com";
    out << YAML::Key << "phone";
    out << YAML::Value << 123456789;
    out << YAML::EndMap;
    out << YAML::EndMap;
    out << YAML::EndMap;

    // 将文档对象写入文件中
    std::ofstream fout("config.yaml");
    fout << out.c_str();
    fout.close();

    return 0;
}

存储结果:

person:
    name: John
    age: 22
    contact:
        email: john@example.com
        phone: 123456789

总结

到这里,我们已经介绍了C++读写YAML配置文件的完整攻略,并且给出了两个实际的示例,希望读者可以通过本文快速了解如何在C++中读写YAML格式的配置文件。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:c++ 读写yaml配置文件 - Python技术站

(1)
上一篇 2023年5月25日
下一篇 2023年5月25日

相关文章

  • Spring Cloud详解实现声明式微服务调用OpenFeign方法

    下面是“Spring Cloud详解实现声明式微服务调用OpenFeign方法”的完整攻略。 1. 前置知识 在介绍如何使用OpenFeign进行声明式微服务调用之前,需要先了解以下内容: 1.1 微服务架构 微服务架构是一种将应用程序拆分为一组较小、独立、自治的服务的方法。每个服务都运行在其独立的进程中,可以使用轻量级机制(如HTTP资源API)进行通信。…

    人工智能概览 2023年5月25日
    00
  • 解决django同步数据库的时候app models表没有成功创建的问题

    当使用Django时,我们通常使用ORM来建立数据库模型。有时,在执行同步数据库命令(如python manage.py migrate)时,可能会遇到一些问题。其中一个常见的问题是在同步时,某个应用的数据库模型未在数据库中创建。 在大多数情况下,这个问题可能与应用配置或模型定义有关。下面是两种可能的解决方法。 1.检查应用配置 应用配置文件是apps.py…

    人工智能概览 2023年5月25日
    00
  • Python OpenCV中cv2.minAreaRect实例解析

    Python OpenCV中cv2.minAreaRect实例解析 介绍 OpenCV是一组用于计算机视觉和机器学习任务的开源库。它提供了许多用于图像处理的函数和工具。cv2.minAreaRect是OpenCV中的一个函数,它可以找到指定二维点集的最小包围矩形。这个函数可以应用于各种类型的应用程序,包括物体检测、人脸识别和图像分析等等。 在这个攻略中,我们…

    人工智能概览 2023年5月25日
    00
  • Python执行Linux系统命令的4种方法

    Python执行Linux系统命令的4种方法 在Python中,我们可以使用多种方式执行Linux系统命令,以下是具体的4种方法: 方法1:os.system() os.system() 方法可以在Python程序中执行Linux系统命令。 import os os.system(‘ls -l’) 以上代码会在控制台输出ls -l命令的结果。 方法2:sub…

    人工智能概览 2023年5月25日
    00
  • 如何解决python多种版本冲突问题

    如何解决Python多种版本冲突问题? Python是一种非常灵活的编程语言,由于其开源及友好社区,使其成为各种类型项目中的首选语言。但是在使用Python时可能会遇到版本冲突的问题。这种情况经常发生在需要多个项目使用不同版本的Python的情况下。下面我们将提供一些解决方案以解决Python多种版本冲突问题。 使用虚拟环境 使用虚拟环境是解决Python版…

    人工智能概览 2023年5月25日
    00
  • Python实现字符串逆序输出功能示例

    实现字符串逆序输出是Python中非常基础的操作。下面我会提供两种示例,来详细讲解如何使用Python实现这个功能。 示例一 第一种方法是使用Python内置的slice(切片)方法。代码如下: string = "hello world" reversed_string = string[::-1] print(reversed_str…

    人工智能概览 2023年5月25日
    00
  • python 判断txt每行内容中是否包含子串并重新写入保存的实例

    针对“Python 判断txt每行内容中是否包含子串并重新写入保存”的问题,可以通过以下几个步骤实现: 1. 读取文件 需要首先先读取该txt文件中的内容,具体实现方法如下: with open(‘file.txt’, ‘r’) as f: lines = f.readlines() 其中,with语句可以自动帮我们关闭文件。通过readlines()函数,…

    人工智能概论 2023年5月25日
    00
  • Docker 部署HAProxy v2.2.29 并暴露指标接口的问题解决

    下面我将详细讲解“Docker 部署HAProxy v2.2.29 并暴露指标接口的问题解决”的完整攻略。 准备工作 首先需要安装Docker,如果已经安装可以跳过这一步。 示例一:在Ubuntu系统上安装Docker # 添加Docker GPG密钥 curl -fsSL https://download.docker.com/linux/ubuntu/g…

    人工智能概览 2023年5月25日
    00
合作推广
合作推广
分享本页
返回顶部