Formatting text in C++: Old and new ways

When it comes to formatting text in C++, there are several options available. The traditional methods include using I/O streams like std::stringstream or the printf family of functions. However, the newer C++20 format library, particularly std::format and std::format_to, is considered to be the modern way of formatting text. In terms of performance, the format library is generally faster than stream operations and sprintf. However, it is important to measure performance for specific cases rather than relying on generalizations. A comparison of different formatting methods shows that std::format is about 5 times faster than std::stringstream and performs similarly to sprintf. It is worth noting that there are some differences in formatting compatibility between std::stringstream and std::format, which may require additional work in certain cases. Additionally, C++23 introduces std::print and std::println for easier formatting when writing to the output console.

https://mariusbancila.ro/blog/2023/09/12/formatting-text-in-c-the-old-and-the-new-ways/

To top