Learn C++
#include "ioformatting/format.hpp"

Modern Formatting with std::format (C++20)

//concept

C++20's std::format (from <format>) finally gives C++ Python-style {} placeholders: std::format("{} + {} = {}", 1, 2, 3) builds the string directly — type-safe like iostreams but far more compact than manipulator chains. Format specs inside the braces control width, alignment, precision, and base ({:08.2f}, {:x}, {:>10}).

The catch is compiler support: GCC 13+, Clang 14+, and MSVC 19.29+ cover it, older toolchains don't — check __cpp_lib_format or your compiler version before depending on it. Where supported, prefer it over both iomanip soup and unsafe printf.

//how it works
1

{} placeholders in order

std::format("{} is {}", name, age) substitutes arguments left to right — no counting codes, no type mismatches possible.

2

Format specs after a colon

{:.2f} fixes two decimals, {:>8} right-aligns in width 8, {:04d} zero-pads — one compact grammar for everything iomanip did verbosely.

3

Type-safe by construction

Mismatched types fail at compile time, unlike printf's runtime undefined behavior on a wrong % code.

4

Check support first

Needs a recent compiler (g++ 13+ with -std=c++20); on older ones, fmt::format from the {fmt} library is the near-identical fallback.

//example
#include <format>
#include <iostream>
 
int main() {
std::string msg = std::format("{} scored {:.1f}", "Ada", 97.25);
std::cout << msg << std::endl;
return 0;
}
Output
Ada scored 97.2
1The first {} takes the string Ada, the second takes the double with a :.1f spec.
2:.1f rounds 97.25 to one decimal place, producing 97.2.
3format returns a ready std::string — no stream, no manipulators, one expression.
//use cases
Building user-facing messages without iomanip chains or string + conversions
Zero-padded, aligned, precision-controlled output in one format string
Replacing every printf with a variant the compiler actually type-checks
Falling back to the {fmt} library when stuck on a pre-C++20 toolchain

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.