Skip to content

std::format(C++20)

一句话

类型安全的 printf 替代品——用 {} 占位符格式化字符串,编译期检查参数数量,支持自定义类型格式化。

头文件

#include <format>

核心 API 速查

操作签名说明
格式化字符串string format(fmt, args...)返回格式化后的字符串
格式化到输出void vformat_to(out_it, fmt, args)输出到迭代器
格式化到缓冲区size_t formatted_size(fmt, args...)预计算输出长度
格式化到 stdout(C++23) void print(fmt, args...)直接输出到标准输出
位置参数"{0} {1} {0}"按序号引用参数
宽度/精度"{:>10.2f}"右对齐、宽度 10、精度 2
自定义格式化template<> struct formatter<T>特化 std::formatter 支持自定义类型

最小示例

cpp
// Standard: C++20
#include <format>
#include <iostream>
#include <string>

int main() {
    std::string s = std::format("Hello, {}!", "world");
    std::cout << s << "\n"; // Hello, world!

    int version = 2;
    double pi = 3.14159265;
    std::cout << std::format("v{}. pi={:.2f}", version, pi) << "\n";
    // v2. pi=3.14

    // 位置参数
    std::cout << std::format("{0} + {0} = {1}", 3, 6) << "\n";
    // 3 + 3 = 6
}

嵌入式适用性:中

  • 替代 printf,消除格式字符串与参数类型不匹配的运行时崩溃风险
  • 替代 std::stringstream,避免堆分配开销
  • 编译期检查参数数量,但格式说明符的完整编译期验证需要 C++23 的 std::is_constant_evaluated 配合
  • Flash 开销可能较大(格式化引擎代码量),资源极度受限设备需评估
  • 可用 {fmt} 库作为 C++11 起的后备方案

编译器支持

GCCClangMSVC
131719.29

另见


部分内容参考自 cppreference.com,采用 CC-BY-SA 4.0 许可

基于 VitePress 构建