{"id":51305246,"url":"https://github.com/paulkazusek/std_format_cheatsheet","last_synced_at":"2026-06-30T23:03:44.233Z","repository":{"id":186820639,"uuid":"380031433","full_name":"paulkazusek/std_format_cheatsheet","owner":"paulkazusek","description":"Cheatsheet for the c++20 format library","archived":false,"fork":false,"pushed_at":"2025-05-23T07:17:29.000Z","size":156,"stargazers_count":47,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-23T08:44:46.185Z","etag":null,"topics":["cpp","cpp20","format","formatter","modern-cpp"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulkazusek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-06-24T19:31:52.000Z","updated_at":"2025-05-23T07:17:33.000Z","dependencies_parsed_at":"2025-03-12T21:37:57.255Z","dependency_job_id":null,"html_url":"https://github.com/paulkazusek/std_format_cheatsheet","commit_stats":null,"previous_names":["paulkazusek/std_format_cheatsheet"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/paulkazusek/std_format_cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulkazusek%2Fstd_format_cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulkazusek%2Fstd_format_cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulkazusek%2Fstd_format_cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulkazusek%2Fstd_format_cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulkazusek","download_url":"https://codeload.github.com/paulkazusek/std_format_cheatsheet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulkazusek%2Fstd_format_cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34986249,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-30T02:00:05.919Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp20","format","formatter","modern-cpp"],"created_at":"2026-06-30T23:03:42.963Z","updated_at":"2026-06-30T23:03:44.221Z","avatar_url":"https://github.com/paulkazusek.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# std::format cheatsheet\n\n![std::format cheatsheet](https://i.imgur.com/tDqRjOx.png)\n\nIf you like this content, or if it helps you, leave a ⭐!\n\n## Introduction\n\n### What is std::format?\n\n`std::format` is a text formatting library introduced in **C++20**, offering a **safe** and **extensible** alternative to the `printf` family of functions. It aims to complement the existing C++ I/O streams library by reusing some of its infrastructure, such as overloaded insertion operators for user-defined types.\n\nPrior to C++20, formatting text in C++ was often **error-prone** and **inconsistent**, relying on functions like `printf` or stream-based formatting. `std::format` was introduced to provide a **safer**, **more consistent**, and **modern** approach to text formatting.\n\nUnlike `printf`, `std::format` offers:\n\n- ✅ **Type safety**\n- ✅ **Compile-time format string checking**\n- ✅ **A clean, modern syntax**\n- ✅ **Integration with user-defined types** through overloaded insertion operators\n\n### Formatting functions\n\n#### std::format\n\n```cpp\nstd::string result = std::format(format_string, args...);\n```\n\n**Description**: Formats the given arguments (args...) according to the format_string and returns the result as a std::string.\n\n[cppreference.com: std::format](https://en.cppreference.com/w/cpp/utility/format/format)\n\n#### std::format_to\n\n```cpp\nauto it = std::format_to(output_iterator, format_string, args...);\n```\n\n**Description**: Formats the arguments just like std::format, but writes the result directly to an output buffer using an output iterator.\n\n[cppreference.com: std::format_to](https://en.cppreference.com/w/cpp/utility/format/format_to)\n\n#### std::format_to_n\n\n```cpp\nauto result = std::format_to_n(output_iterator, n, format_string, args...);\n```\n\n**Description**: Like format_to, but limits the output to a maximum of n characters. Returns a pair consisting of an output iterator pointing past the last written character and the number of characters that would have been written.\n\n[cppreference.com: std::format_to_n](https://en.cppreference.com/w/cpp/utility/format/format_to_n)\n\n#### std::formatted_size\n\n```cpp\nsize_t size = std::formatted_size(format_string, args...);\n```\n\n**Description**: Returns the number of characters that would be produced by std::format (without actually formatting or allocating memory). Useful for pre-allocating buffers.\n\n[cppreference.com: std::format_size](https://en.cppreference.com/w/cpp/utility/format/formatted_size)\n\n## Table of Contents\n\n- [std::format in C++20](#stdformat-in-c20)\n\t* [Introduction](#introduction)\n\t\t+ [What is std::format?](#what-is-stdformat)\n\t\t+ [Formatting functions](#formatting-functions) \n\t* [Table of contents](#table-of-contents)\n\t* [Requirement](#Requirement)\n\t* [Currently ways to formatting in C++](#currently-ways-to-formatting-in-c)\n\t\t+ [C-Style printf() from \\\u003ccstdio\\\u003e](#c-style-printf-from-)\n\t\t+ [C++ I/O streams](#c-io-streams)\n\t\t+ [Boost Format](#boost-format)\n\t\t+ [Fast Format](#fast-format)\n\t\t+ [std::format in \\\u003cformat\\\u003e](#stdformat-in-format)\n\t* [Placeholder](#placeholder)\n\t\t+ [Brace-delimited replacement fields](#brace-delimited-replacement-fields)\n  \t* [Format specifiers](#format-specifiers)\n\n\n## Requirement\n\n```cpp\n#include \u003cformat\u003e\n ```\n\n| compiler support | Text Formatting (C++20)  | status |\n|:--------:|:-------------:|:-------------:|\n| Visual Studio | [VS 2019 16.10 / cl 19.29](https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-170#c-standard-library-features) | completed |\n| GCC | [13.1](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2020) | completed |\n| clang | [14](https://libcxx.llvm.org/Status/Cxx20.html) | completed |\n\nSource: [cppreference](https://en.cppreference.com/w/cpp/20)\n   \n## Currently ways to formatting in C++\n\n### printf() function inherited from C\n\n```cpp\n#include \u003ccstdio\u003e\t// C library for Input/Output operations\n ```\n\n```cpp\nconst int answer { 42 };\n\nprintf( \"The answer is %d \", answer );\n ```\n\n### C++ I/O streams\n\n```cpp\nconst int answer { 42 };\n\nstd::cout \u003c\u003c \"The answer is \" \u003c\u003c answer \u003c\u003c \"\\n\";\n ```\n \n### Boost Format\n \n```cpp\nconst int answer { 42 };\n\nstd::cout \u003c\u003c boost::format( \"The answer is %\") % answer;\n ```\n\n[www.boost.org](https://www.boost.org/doc/libs/1_77_0/libs/format/doc/format.html)\n \n### Fast Format\n \n```cpp\nconst int answer { 42 };\n\nff::fmtll( std::cout,  \"The answer is {0}\", answer );\n ```\n\n[www.fastformat.org](http://www.fastformat.org/) or [github.com/synesissoftware/FastFormat](https://github.com/synesissoftware/FastFormat)\n \n### std::format in \\\u003cformat\\\u003e\n \n```cpp\nconst int answer { 42 };\n\nstd::cout \u003c\u003c std::format( \"The answer is {}\", answer );\n ```\n \n## Placeholder\n\nThe {} indicates a replacement field like % in printf. \n \n### Brace-delimited replacement fields\n \n```cpp\nstd::cout \u003c\u003c std::format( \"The answer is {}\", 42 );\n ```\nWrites the following output:\n\n```bash\nThe answer is 42\n ```\n \n### Positional arguments\n \n ```cpp\nstd::cout \u003c\u003c std::format( \"I'd rather be {1} than {0}\", \"right\", \"happy\" );\n ```\n \n### curly braces in output\n \n ```cpp\nstd::cout \u003c\u003c std::format( \"The answer is {{ }}\", 42 );\n ```\n \n## Format specifiers\n \nPlaceholder can contains a format specifiers. It starts with a colon: {[index]:[format specifiers]}\n \n[[fill]align][sign][#][0][width][.precision][type]\n \n### [width]\n \nminimum desired field width\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:5}\", 42 );\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:{}}\", 42, 5 );\n ```\n ### [[fill]align]\n \nSpecifies optional characters and aligment\n\n### Aligment\n\u003cul\u003e\n \u003cli\u003e\u003cp\u003e\u003c left\u003c/p\u003e\u003c/li\u003e\n \u003cli\u003e\u003cp\u003e\u003e right\u003c/p\u003e\u003c/li\u003e\n \u003cli\u003e\u003cp\u003e^ center\u003c/p\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:\u003c20}\", \"left\");\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:\u003e20}\", \"right\");\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:^20}\", \"centered\");\n ```\n ### Fill and aligment\n \n ```cpp\nstd::cout \u003c\u003c std::format( \"{:-^20}\", \"centered\");\n ```\n \n### [sign]\n \n\u003cul\u003e\n \u003cli\u003e\u003cp\u003e- sign only for negative numbers (default)\u003c/p\u003e\u003c/li\u003e\n \u003cli\u003e\u003cp\u003e+ sign for negative and positive numbers\u003c/p\u003e\u003c/li\u003e\n \u003cli\u003e\u003cp\u003escape display minus sign for negative numbers, a space for positive numbers\u003c/p\u003e\u003c/li\u003e\n\u003c/ul\u003e\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:\u003c5}\", 42 );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:\u003c+5}\", 42 );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:\u003c 5}\", 42 );\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:\u003c 5}\", -42 );\n ```\n\n### [#]\n \n\u003cul\u003e\n \u003cli\u003e\u003cp\u003eenabled alternative formatting rules\u003c/p\u003e\u003c/li\u003e\n \u003cli\u003e\u003cp\u003eintegral types\u003c/p\u003e\n  \u003cul\u003e\n     \u003cli\u003eHexadecimal format: inserts 0x or 0X at font\u003c/li\u003e\n     \u003cli\u003eBinray format: inserts 0b or 0B at font\u003c/li\u003e\n     \u003cli\u003eOctal format: inserts 0\u003c/li\u003e\n    \u003c/ul\u003e\n \u003c/li\u003e\n \u003cli\u003e\u003cp\u003efloating-points types\u003c/p\u003e\n  \u003cul\u003e\n   \u003cli\u003ealways show decimal separator, even without following digits\u003c/li\u003e\n  \u003c/ul\u003e\n \u003c/li\u003e\n\u003c/ul\u003e\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:15d}\", 42 );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:15b}\", 42 );\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:#15b}\", 42 );\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:15X}\", 42 );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:#15X}\", 42 );\n ```\n \n### [type]\n\n#### integer types\n#### floating-points types\n#### booleans\n#### characters\n#### strings\n#### pointers\n\n\n### [.precision]\n \nonly for floating-points types and strings types\n\n```cpp\nconst double pi { 3.1415 };\nconst int precision { 2 };\nconst int width { 15 };\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( \"{:15.2f}\", pi );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:15.{}f}\", pi, precision );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:{}.{}f}\", pi, width, precision );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{0:{1}.{2}f}\", pi, width, precision );\n ```\n\n## Localization\n\t\nstd::locale is supported in std::format \n \n\u003e **Note:** Use `:L` to enable locale specific formatting for an argument.\n \n```cpp\n#include \u003clocale\u003e\n```\n\n```cpp\nstd::cout \u003c\u003c std::format( locale( \"en_US.UTF-8\" ), \"{:L}\", 1024 );\n ```\n\n```cpp\nstd::cout \u003c\u003c std::format( locale( \"zh_CN.UTF-8\" ), \"{:L}\", 1024 );\n ```\n \n```cpp\nstd::cout \u003c\u003c std::format( locale( \"de_DE.UTF-8\" ), \"{:L}\", 1024 );\n ```\n\n## formating std::chrono\n \n```cpp\n#include \u003cchrono\u003e\n```\n\n### Example 1\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{:%d.%m.%Y}.\", std::chrono::system_clock::now() ) \u003c\u003c \"\\n\";\n```\n\n### Example 2\n\n```cpp\nvoid print_happy_birthday( const std::string_view\u0026 name, const std::chrono::year_month_day\u0026 birthday )\n{\n\tstd::cout \u003c\u003c std::format( \"{0}'s birthday is {1:%Y-%m-%d}.\", name, birthday ) \u003c\u003c \"\\n\";\n}\n```\n\t\n```cpp\nusing namespace std::chrono;\nyear_month_day birthday { 1976y / February / 1 };\nstd::string name { \"Max Mustermann\" };\n\nprint_happy_birthday( name , birthday );\n```\n\t\n## formating std::complex\n\t\n```cpp\n#include \u003ccomplex\u003e\n```\n\n```cpp\nstd::complex\u003cdouble\u003e c { 4.0, 8.0 };\n```\n\n```cpp\nstd::cout \u003c\u003c std::format( \"{}\", c );\n```\n\n## Formatting User-Defined Types\n\nSeperate parsing and formatting in extension API\n\nNeed to provide a specialization of std::formater\\\u003c\\\u003e and imlpement\n\n\u003cul\u003e\n \u003cli\u003eformatter\u003c\u003e::parse()\u003c/li\u003e\n \u003cli\u003eformatter\u003c\u003e::format()\u003c/li\u003e\n\u003c/ul\u003e\n\n### Example Point\n\n```cpp\n#include \u003cformat\u003e\n#include \u003ciostream\u003e\n\nstruct Point {\n    int x, y;\n};\n\ntemplate \u003c\u003e\nstruct std::formatter\u003cPoint\u003e {\n    constexpr auto parse( format_parse_context\u0026 context ) {\n        return context.begin();\n    }\n\n    template \u003ctypename FormatContext\u003e\n    auto format( const Point\u0026 point, FormatContext\u0026 context ) {\n        return std::format_to( context.out(), \"({}, {})\", point.x, point.y );\n    }\n};\n\nint main() {\n    Point point{3, 7};\n\n    // Format to string\n    std::string formatted_point = std::format( \"The point is: {}\", point );\n    std::cout \u003c\u003c formatted_point \u003c\u003c '\\n';\n\n    // Direct output using std::format_to and std::back_inserter\n    std::string buffer;\n    std::format_to( std::back_inserter( buffer ), \"Formatted directly: {}\\n\", point );\n    std::cout \u003c\u003c buffer;\n\n    return 0;\n}\n```\n\n### Example Person\n\n```cpp\nclass Person {\npublic:\n\tPerson() = delete;\n\tPerson( unsigned long long id, const std::string\u0026 firstName, const std::string\u0026 lastName ) noexcept\n\t\t: _id( id ), _firstName( firstName ), _lastName( lastName ) {}\n\n\tauto getId() const noexcept -\u003e unsigned long long {\n\t\treturn _id;\n\t}\n\tauto getFirstName() const noexcept -\u003e const std::string\u0026 {\n\t\treturn _firstName;\n\t}\n\tauto getLastName() const noexcept -\u003e const std::string\u0026 {\n\t\treturn _lastName;\n\t}\n\nprivate:\n\tunsigned long long _id;\n\tstd::string _firstName;\n\tstd::string _lastName;\n};\n\ntemplate\u003c\u003e\nclass std::formatter\u003cPerson\u003e {\npublic:\n\tconstexpr auto parse( auto\u0026 context ) {\n\t\n\t}\n\t\n\tauto format( const Person\u0026 person, auto\u0026 context ) {\n\t\n\t}\n};\n```\n\n## Enum to String Formatting\n\n```cpp\n#include \u003cformat\u003e\n#include \u003ciostream\u003e\n#include \u003cstring_view\u003e\n\nenum class Direction { North, East, South, West };\n\nstd::string_view to_string( Direction direction ) {\n    switch (direction) {\n\tusing enum Direction;\n        case North: return \"north\";\n        case East:  return \"east\";\n        case South: return \"south\";\n        case West:  return \"west\";\n        default:    return \"unknown\";\n    }\n}\n\ntemplate\u003c\u003e\nstruct std::formatter\u003cDirection\u003e : std::formatter\u003cstd::string_view\u003e {\n    auto format( Direction direction, auto\u0026 context ) const {\n        return std::formatter\u003cstd::string_view\u003e::format( to_string( direction ), context );\n    }\n};\n\nint main() {\n    Direction direction = Direction::East;\n    std::cout \u003c\u003c std::format( \"Direction: {}\",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CizReHijheAUEANCytrFjgWAYEQFBUEPOg4nISg0HG%2Bh4mAKQzD4Ogn26iBoja0hojCBoAE9OEy7bmGIXaAHlom0c0Dt4Ga2EEU6GFofactILBoi8YA3DEWhuu4XgsHOIxxBe9yoXZTaCIVUt1li6Sqk22g8GiYg9o8LBNuAvAWE2sHiGiFJMGxA9DGARGjH6vgDGABQADUxVOxJGGumRBBEMR2AqfhBEUFR1Be3RAgMcnTCSyx9CR7rIEWVBEhqX79gAWn5TSRasSwzA0RXTsy1BcZArBJfk9odL8CBXFGPxAhCaYSjKPQ8jSAQLftlJHYYXpbbmY20K6EZPBaPQ7BN32pmKfpyiGbpncCCZug98OJEWGsVjWROmo4SLSGi2KOv2VQyuwhXsMkfZgGQZB9ikIqzBNXBCBIcNNi4eZeGyrR5kWBBPiweIjfyyRiskTZNmL7DfKJDRfI0SQKnCjgWtIbGapKswyoATjKyQh7MNeuE2aqs82jqup6vqcoG4aICQJlElLKaIBmxIJuICJWHWfPC%2BL0vy8ryRq94TA%2BAiD6z0FzYQohxCc1kDzNQm0BakDhCjRI10woRVai9Dqp1Sy3yIqgKgecC5FxLmXCuVca4QA8LNOIjdm6t36p3buAwjZzwXtjMwvkiplSJFwDQXBfK%2BWwhPAuRJNiHwwZwE%2BvU265VIP3IknCiQVTKtVdhu8uB73TpsdBOcJFn3bqgjgZhtHtV0dIjupBcapGcJIIAA%3D%3D%3D)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulkazusek%2Fstd_format_cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulkazusek%2Fstd_format_cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulkazusek%2Fstd_format_cheatsheet/lists"}