• 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
  • 相关阅读:
    C#底层库--操作Excel帮助类(读取、导出表格)
    Zookeeper和Kafka on k8s 部署
    怎么隐藏服务器的ip地址
    Frp内网穿透win系统实录
    20221118 今天的世界发生了什么
    基于Java毕业设计大学生兼职招聘网站源码+系统+mysql+lw文档+部署软件
    C++语言之输入输出介绍
    Unity 3D 导航系统||Unity 3D 障碍物
    Java实现停车场收费系统 JAVA+Vue+SpringBoot+MySQL
    33 WEB漏洞-逻辑越权之水平垂直越权全解
  • 原文地址:https://blog.csdn.net/weixin_44347020/article/details/133684871