温馨提示×

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

c++
小樊
96
2024-08-11 06:21:35
栏目: 编程语言
C++开发者专用服务器限时活动,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标准。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

0