码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c++ fmt


    fmt 是一个文本格式库,可以代替 C 的 stdio 和 C++ iostreams。实现了 C++20 的 std::format 标准。

    格式化输出

    #include 
    
    int main() {
      fmt::print("Hello, world!\n");
      fmt::print("Hello, {}!", "world");
      fmt::print("Hello, {0}, {0}", "world");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    format函数

    fmt::memory_buffer buf;
    fmt::format_to(buf, "{}", 42);
    fmt::format_to(buf, "{:x}", 42); // 十六进制表示
    std::cout << buf.data();
    
    • 1
    • 2
    • 3
    • 4

    格式字符串

    std::string s = fmt::format("The answer is {}.", 42);
    // s == "The answer is 42."
    
    • 1
    • 2

    保留2位小数

    std::string s = fmt::format("The answer is {:.2f}", 1.12345678);
    // s == "The answer is 1.12"
    
    • 1
    • 2

    使用位置参数

    std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");
    // s == "I'd rather be happy than right."
    
    • 1
    • 2

    使用别名参数

    fmt::print("Hello, {name}! The answer is {number}. Goodbye, {name}.",
               fmt::arg("name", "World"), fmt::arg("number", 42));
    
    • 1
    • 2

    时间日期格式化

    #include 
    
    int main() {
      using namespace std::literals::chrono_literals;
      fmt::print("Default format: {} {}\n", 42s, 100ms);
      fmt::print("strftime-like format: {:%H:%M:%S}\n", 3h + 15min + 30s);
      std::time_t t = std::time(nullptr);
      fmt::print("The date is {:%Y-%m-%d}.\n", *std::localtime(&t));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出列表

    #include 
    #include 
    
    int main() {
      std::vector<int> v = {1, 2, 3};
      fmt::print("{}\n", v);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    输出到文件

    #include 
    
    int main() {
      auto out = fmt::output_file("guide.txt");
      out.print("Don't {}", "Panic");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出使用特定颜色

    #include 
    
    int main() {
      fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold,
                 "Hello, {}!\n", "world");
      fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) |
                 fmt::emphasis::underline, "Hello, {}!\n", "мир");
      fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
                 "Hello, {}!\n", "世界");
      fmt::print(fmt::color::red, "hello {}\n", "world");
      fmt::print(fmt::rgb(10, 50, 63), "hello {}\n", "world");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    error 输出

    fmt::memory_buffer error_buff;
    fmt::format_error_code(error_buff, 42, "test");
    std::cout << error_buff.data() << std::endl;
    // test: error 42
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    【SQL Server + MySQL二 】SQL: DDL数据定义【定义、修改、删除基本表】,DML【憎删改查】,DCL数据控制语言
    【疑难】使用ARM development studio仿真 error:Failed to create Jython interpreter
    Java设计模式:你见过大厂是怎么玩单列模式的吗?
    ES6基础语法(let、const、解构赋值、模板字符串、简化对象、箭头函数、扩展运算符)(一)
    找不到msvcp140.dll无法继续执行代码是什么意思与解决方法
    Redis-数据类型-List
    使用ssh在本地环境(Windows)连接虚拟机以及其中的docker容器
    正则匹配字符串中电话号码的中间几位为指定字符
    几分钟实现对恶意IP地址进行拦截,腾讯云Web防火墙实在太香了!
    阿里云国际站:阿里云linux扩充磁盘大小常见问题
  • 原文地址:https://blog.csdn.net/weixin_44347020/article/details/133684871
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号