Python 中 % 或 .format 运算符的 C++ 等价物是什么?

2 c++ python python-3.x fmt

我对 C++ 很陌生,我正在编写一个程序,该程序需要一个与 Python % 运算符执行相同操作的运算符。C++ 中是否有任何等价物?

jtb*_*des 7

C++20std::format库用于此目的:

#include <iostream>
#include <format>
 
int main() {
    std::cout << std::format("Hello {}!\n", "world");
}
Run Code Online (Sandbox Code Playgroud)

有关如何使用它的更多信息和指南,请参阅:

但是,<format>在某些标准库实现中尚未提供 — 请参阅C++20 库功能。同时,您可以使用https://github.com/fmtlib/fmt,它是等效的(并且是 的灵感<format>)。