温馨提示×

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

c++
小樊
88
2024-08-11 06:21:35
栏目: 编程语言

在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标准。

0