Site Tools


wiki:hpp-format

Table of Contents

<format>

<format> provides std::format, a type-safe, fast, printf-style formatting function introduced in C++20. It replaces most uses of sprintf, string streams, and std::cout manipulators.

It supports compile-time format string checking (in some implementations) and is faster than stream-based formatting.

Example

This example demonstrates type-safe formatting with automatic type deduction, creating formatted output with precision control.

// compile: g++ -std=c++20 -o formatexample formatexample.cpp
// run: ./formatexample
// description: type-safe formatting with std::format
 
#include <format>
#include <iostream>
 
int main() {
    int x = 42;
    double pi = 3.14159;
    std::string name = "Alice";
 
    std::cout << std::format("hello {}, x={}, pi={:.2f}\n", name, x, pi);
 
    return 0;
}
wiki/hpp-format.md · Last modified: by 127.0.0.1