如何在c++中使用std::format

avatar
作者
筋斗云
阅读量:0

在C++20中,可以使用std::format函数来格式化字符串。例如:

#include <iostream> #include <format>  int main() {     std::string name = "Alice";     int age = 30;      std::string formattedString = std::format("My name is {} and I am {} years old.", name, age);      std::cout << formattedString << std::endl;      return 0; } 

这将输出:

My name is Alice and I am 30 years old. 

请注意,要使用std::format函数,您需要在编译时使用支持C++20的编译器,并启用C++20标准。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!