2

I am quite new to C++ and I am writing a program that needs an operator that does the same thing as the Python % operator. Is there any equivalent in C++?

vitaut
  • 49,672
  • 25
  • 199
  • 336

3 Answers3

7

The C++20 std::format library serves this purpose:

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

For more info and guides on how to use it, see:

However, <format> isn't provided in some standard library implementations yet — see C++20 library features. In the meantime you can use https://github.com/fmtlib/fmt, which is equivalent (and was the inspiration for <format>).

vitaut
  • 49,672
  • 25
  • 199
  • 336
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • When I try to compile your code in VS 2019, I get these three errors: cannot open "format", namespace std has no member format and: cannot open include file "format": no such file or directory –  Jul 26 '20 at 21:28
  • See: https://stackoverflow.com/questions/61561306/c20-support-in-visual-studio – jtbandes Jul 26 '20 at 21:31
3

C++ has several ways to do IO, mostly for historical reasons. Whichever style your project uses should be used consistently.

  1. C-style IO: printf, sprintf, etc.
#include <cstdio>

int main () {
  const char *name = "world";
  // other specifiers for int, float, formatting conventions are avialble
  printf("Hello, %s\n", name); 
}
  1. C++ style IO: iostreams
#include <iostream>

int main() {
  std::string name = "world";
  std::cout << "Hello, " << name << std::endl;
}
  1. Libraries/C++20 std::format:

Pre C++20 quite a few people have provided their own formatting libraries. One of the better ones is {fmt}. C++ adopted this kind of formatting as [std::format][2]

#include <format>
#include <iostream>
#include <string>

int main() {
  std::string name = "world";
  std::cout << std::format("Hello, {}", name) << std::endl;
}

Notice that format produces format-strings, so it works with both ways to do IO, and/or other custom approaches, but if you use C-style IO it would probably be mostly weird to layer std::format on top, where the printf specifier would also work.

vitaut
  • 49,672
  • 25
  • 199
  • 336
midor
  • 5,487
  • 2
  • 23
  • 52
0
printf("%i", 123456789);
  • Not really the same as in python – smac89 Jul 26 '20 at 21:35
  • @smac89 c++ isn't python (also it's a lot better than python) –  Jul 26 '20 at 21:36
  • I know this (although I'm not sure about "better"), I'm just saying that the way you have used `%` is not the same as what OP is looking to emulate, as in python. The answer you have given is more similar to how one would use the `{}` format in python, than the way `%` works in python – smac89 Jul 26 '20 at 21:39
  • Then maybe OP should instead ask "why doesn't python look as good as c++ does" –  Jul 26 '20 at 21:41
  • Actually this works the same as `%` operator in . Nvm my previous comment. I've long been away from python and C++ – smac89 Jul 26 '20 at 21:50
  • Oh good, now we can ignore the part where you stated that python is better than c++. Finally. –  Jul 26 '20 at 21:50