Formatting and printing text with std::format and std::print
The C++ language has two ways of formatting text: the printf
family of functions and the I/O streams library. The printf
functions are inherited from C and provide a separation of the formatting text and the arguments. The streams library provides safety and extensibility and is usually recommended over printf
functions, but is, in general, slower. The C++20 standard proposes a new formatting library alternative for output formatting, which is similar in form to printf
but safe and extensible and is intended to complement the existing streams library. In this recipe, we will learn how to use the new functionalities instead of the printf
functions or the streams library.
Getting ready
The new formatting library is available in the <format>
header. You must include this header for the following samples to work.
How to do it...
The std::format()
function formats its arguments according to the provided...