0
点赞
收藏
分享

微信扫一扫

【Java 的四大引用详解】

天行五煞 2024-07-24 阅读 28

文章目录

😏1. 项目介绍

项目Github地址:https://github.com/fmtlib/fmt

fmt 是一个现代化的 C++ 格式化库,旨在提供高性能、安全、易用的文本格式化功能。它支持类似于 Python 的字符串格式化语法,并且能够直接与标准输出流和字符串进行交互。

主要特点和功能:

😊2. 环境配置

Ubuntu可以apt安装fmt:

sudo apt install libfmt-dev

程序g++编译:

g++ -o main main.cpp -lfmt

😆3. 使用说明

简单字符串输出示例:

#include <fmt/core.h>
#include <iostream>

int main() {
    std::string name = "Bob";
    int age = 30;
    fmt::print("Hello, {}! You are {} years old.\n", name, age);
    return 0;
}

复杂字符串输出示例(对齐、数字格式化):

#include <fmt/core.h>
#include <fmt/format.h>
#include <iostream>
#include <vector>

struct Person {
    std::string name;
    int age;
};

int main() {
    std::vector<Person> people = {
        {"Alice", 30},
        {"Bob", 25},
        {"Charlie", 35}
    };

    // 使用 fmt::format 进行字符串格式化
    std::string output;
    for (const auto& person : people) {
        output += fmt::format("Name: {0:<10} | Age: {1:02}\n", person.name, person.age);
    }

    // 使用 fmt::print 直接输出到标准输出
    fmt::print("List of People:\n{}\n", output);

    return 0;
}

在这里插入图片描述

以上。

举报

相关推荐

0 条评论