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;
}
运行结果如下: