C++ 20新特性之std::format

为什么需要std::format

在C++ 20之前,我们通常使用printf、stringstream、cout等流对象,并结合各种流操作符(比如:<<)和格式化控制符(比如:std::setw、std::setprecision等)来实现格式化输出。这种方式虽然灵活,但使用起来却相当繁琐,特别是在处理复杂的格式化需求时。

在下面的示例代码中,我们使用cout来输出一句文字信息,其中包括姓名和年龄。可以看到,cout的写法比较丑陋和繁琐,不够简洁。

C++ 复制代码
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string strName = "Mike";
    int nAge = 18;
    cout << "Name is " << strName << ", Age is " << nAge << "." << endl;
    return 0;
}

为了解决这一问题,C++ 20中引入了std::format。std::format借鉴了Python中str.format()函数的思想,通过占位符和格式化选项来实现灵活的格式化输出。与printf、cout等相比,std::format具有更强的类型安全性和可读性,同时也支持更多的格式化选项。

基本用法

std::format函数的基本形式如下。

C++ 复制代码
std::string strText = std::format("格式化字符串", 参数1, 参数2, ...);

其中,"格式化字符串"包含普通文本以及由大括号{}包围的占位符,这些占位符会被后面的参数值替换。注意:参数的顺序应当与占位符的顺序保持一致。

下面的示例代码与上面提到的代码功能相同,但使用std::format简化了输出。运行这段代码,将会输出:"Name is Mike, Age is 18.",其中的两个大括号{}被strName和nAge替换。

C++ 复制代码
#include <iostream>
#include <string>
#include <format>
using namespace std;

int main()
{
    string strName = "Mike";
    int nAge = 18;
    cout << format("Name is {}, Age is {}.", strName, nAge) << endl;
    return 0;
}

使用索引

我们还可以通过索引指定参数的位置,这在需要改变输出顺序时非常有用,具体用法可参考下面的示例代码。

C++ 复制代码
#include <iostream>
#include <string>
#include <format>
using namespace std;

int main()
{
    string strName = "Mike";
    int nAge = 18;
    // 索引0对应strName,索引1对应nAge
    cout << format("Name is {0}, Age is {1}.", strName, nAge) << endl;
    // 索引0对应nAge,索引1对应strName
    cout << format("Name is {1}, Age is {0}.", nAge, strName) << endl;
    return 0;
}

格式化数字

std::format支持数字的格式化,比如:保留小数位数、添加前导零等,具体用法可参考下面的示例代码。

C++ 复制代码
#include <iostream>
#include <format>
using namespace std;

int main()
{
    const double PI = 3.14159265;
    // 输出3位小数的PI:3.142
    cout << format("PI: {:.3f}", PI) << endl;
    // 输出前导零,总宽度为5:00066
    cout << format("Number: {:05}", 66) << endl;
    return 0;
}

对齐与填充

std::format引入了一套丰富的格式化选项,允许我们控制字符串的对齐、填充以及其它格式化细节。对齐方法主要有三种:左对齐、居中对齐、右对齐。

{:<width}:左对齐,确保总宽度至少为width,不足部分使用空格填充。

{:>width}:右对齐,确保总宽度至少为width,不足部分使用空格填充。

{:^width}:居中对齐,确保总宽度至少为width,不足部分使用空格填充。

另外,除了使用默认的空格作为填充字符外,我们还可以指定其他字符进行填充,使用方式如下。

{:[fillchar]<width[.precision]}

在下面的示例代码中,我们使用format演示了左对齐、居中对齐、右对齐的效果。其中,<、^、>分别代表左对齐、居中对齐和右对齐,数字10表示总宽度,#代表填充字符。

C++ 复制代码
#include <iostream>
#include <format>
using namespace std;

int main()
{
    // 输出:Left       |   Center   |      Right
    cout << format("{:<10} | {:^10} | {:>10}\n", "Left", "Center", "Right");
    // 输出:66         |    100     |       1024
    cout << format("{:<10} | {:^10} | {:>10}\n", 66, 100, 1024);
    // 输出:|Left######|##Center##|#####Right|
    cout << format("|{:#<10}|{:#^10}|{:#>10}|\n", "Left", "Center", "Right");
    return 0;
}

💡 如果想阅读最新的文章,或者有技术问题需要交流和沟通,可搜索并关注微信公众号"希望睿智"。

相关推荐
澄澈i2 小时前
CMake学习篇[3]---CMake进阶+嵌套CMakeLists+多层级关系
c++·学习·cmake
杨小码不BUG3 小时前
蛇形舞动:矩阵填充的艺术与算法(洛谷P5731)
c++·算法·矩阵·csp-j/s·循环控制
草莓熊Lotso3 小时前
《C++ STL list 完全指南:从基础操作到特性对比,解锁链表容器高效用法》
开发语言·c++·list
繁星星繁4 小时前
C++11学习笔记
c++·笔记·学习
Stanford_11069 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
千里马-horse11 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Lucis__11 小时前
再探类&对象——C++入门进阶
开发语言·c++
北京不会遇到西雅图12 小时前
【SLAM】【后端优化】不同优化方法对比
c++·机器人
jndingxin13 小时前
c++多线程(6)------ 条件变量
开发语言·c++
程序员莫小特13 小时前
老题新解|大整数加法
数据结构·c++·算法