背景:std的cout写起来太烦,printf也是,就不能像python一样智能化一点吗?
可以,C++23已经把fmt安排上了,于是,先用用看吧
1.安装 or 抠库
官网:GitHub - fmtlib/fmt: A modern formatting library
git clone https://github.com/fmtlib/fmt.git
cd fmt && mkdir build && cd build
cmake ..
make -jN #这里的N是你要调用的线程数,如8,16
然后,这里你可以直接抠库,即把build文件夹里make后的libfmt.a带走,加上fmt文件夹下的include里的.h文件,放进你项目里的3rdparty,直接使用(详见我的第一篇博客 C++项目经验(1)——切实理解#include<>和#include““的区别,并利用它“抠库”)
或者,安装到系统
sudo make install
抠库或安装到系统后,CMakeLists的写法不再做赘述(抠库的写法已在(1)中介绍,安装的写法我不用,需要的自行百度)
2.简单使用
#include "fmt/core.h"
#include "fmt/rangs.h"
std::string s{"hello again!"};
double num=42.0;
std::vector<int> arr{1,2,3};
fmt::print("hello, fmt\n"); //打印字符串
fmt::print("{}\n",s); // 打印字符串
fmt::print("{}, it is {:.10f}\n",s,num); //打印保留位数数字
fmt::print("{}\n",arr); //直接打印数组
看起来除了直接打印数组外,其他跟c的printf差别不大?我觉得还是有差别的,主要是更像python了,而且还有一个很重要的format功能,我这里没用上,应该会更像python