我是 C++ 的新手,我正在编写一个程序,该程序需要一个运算符来执行与 Python % 运算符相同的操作。 C++ 中是否有任何等效项?
最佳答案
C++20 std::format
图书馆服务于此目的:
#include <iostream> #include <format> int main() { std::cout << std::format("Hello {}!\n", "world"); }
有关如何使用它的更多信息和指南,请参阅:
- https://en.cppreference.com/w/cpp/utility/format
- https://www.bfilipek.com/2020/02/extra-format-cpp20.html
- https://www.zverovich.net/2019/07/23/std-format-cpp20.html
- How can I use C++20 std::format?
然而,<format>
在某些标准库实现中尚未提供 — 请参阅 C++20 library features .在此期间,您可以使用 https://github.com/fmtlib/fmt ,这是等效的(也是 <format>
的灵感来源)。
关于python - Python 中 % 或 .format 运算符的 C++ 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63105833/