fmtlib 格式化 基本用法 1

12 篇文章 0 订阅

fmtlib 库地址 https://github.com/fmtlib/fmt

是一个开源格式库,为C stdio和C++iostream提供了一个快速安全的格式化替代方案。

#include <iostream>
#include <vector>
#include <fmt/core.h>
using namespace std;

int main(int argc, char** argv)
{
    
    std::string message = fmt::format("The answer is {}.", 42);
    std::cout << message << std::endl;

    //位置参数 Format a string using positional arguments
    std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");
    // s == "I'd rather be happy than right."

    //命名参数
    fmt::print("Elapsed time: {s:.2f} seconds\n", fmt::arg("s", 1.23));

    //输出到vector
    auto out = std::vector<char>();
    fmt::format_to(std::back_inserter(out), "{}", 42);
    std::cout << string(out.begin(), out.end()) << std::endl;

    //输出到 FILE* 标准流
    fmt::print(stderr, "Don't {}!\n", "panic");

    //打印 浮点 inf
    fmt::print("{}", std::numeric_limits<double>::infinity());
    //always prints inf while the output of printf is platform-dependent.

    return 0;
}

运行结果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值