fmt是C++20标准std::format的参考实现和超集,更安全高效;支持编译时检查、自定义类型、日期时间等;可通过vcpkg/Conan安装或单头文件嵌入。

fmt 是一个现代、安全、高性能的 C++ 格式化库,比 std::iostream 更简洁,比 printf 更类型安全。它已被纳入 C++20 标准(std::format),而 fmt 库本身是其参考实现和超集,支持更多功能(如编译时格式检查、自定义类型格式化、日期时间、宽字符等)。
推荐使用 vcpkg 或 Conan 管理依赖,也可直接下载单头文件 fmt/format.h(轻量嵌入):
vcpkg install fmt,CMake 中 find_package(fmt CONFIG)
include/fmt/format.h,直接 #include "fmt/format.h"
最简示例:
#include "fmt/format.h"
#include <iostream>
int main() {
std::string s = fmt::format("Hello, {}! You have {} messages.", "Alice", 42);
std::cout << s << "\n"; // 输出:Hello, Alice! You have 42 messages.
}
fmt 在编译期解析格式字符串,自动匹配参数类型,杜绝 printf 的 %d/%%s 错配或越界访问问题:
立即学习“C++免费学习笔记(深入)”;
// ✅ 安全:类型自动推导,无需手动指定格式符
fmt::print("Value: {}, Name: {}", 3.14, "pi");
// ❌ 编译失败:格式串与参数数量/类型不匹配
// fmt::print("{}", 1, 2); // error: too many args
// fmt::print("{:d}", 3.14); // error: 'd' not valid for double
支持类似 Python 的丰富格式语法,且可扩展:
{:>10} 右对齐占10位,{:^8.2f} 居中、宽度8、保留2位小数{:x}, {:b};带前缀:{:#x} → 0xff
fmt::formatter<t></t>,即可直接格式化
struct Point { int x, y; };
template <> struct fmt::formatter<Point> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const Point& p, FormatContext& ctx) {
return fmt::format_to(ctx.out(), "({},{})", p.x, p.y);
}
};
// 使用:
fmt::print("Origin: {}\n", Point{0, 0}); // Origin: (0,0)
日常开发中可统一用 fmt::print 替代 std::cout 和 <code>printf:
fmt::print(stderr, "[WARN] Failed to open {}: {}\n", path, err.msg());
fmt::format_to 支持写入预分配 buffer,避免临时 string 分配fmt::l10n 模块做本地化格式(如货币、日期)注意:若项目已用 C++20,可先试 std::format(接口几乎一致),但 fmt 功能更全、兼容性更好(支持 C++17+)。
以上就是c++++的fmt库怎么用 比iostream和printf更优秀的格式化库【第三方库】的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号