Dup Ver Goto 📝

StringFormat_01

PT2/lang/cpp/string does not exist
To
22 lines, 67 words, 510 chars Page 'StringFormat_01' does not exist.

C++20

C++20 introduces format(), so we can do the following.

#include <iostream>
#include <format>
#include <cmath>

int main() {
  for(int i=0; i<10; i++) {
    double x = pow(2.0,i);
    std::cout << std::format("Hello {}! {} {:.03f}\n", "world", i, x);
  }
}

Compile with

g++ -o a -std=c++20 a.cpp

(Note the -sdc=c++20 as g++ uses an earlier standard by default.)

Resources