4 ms·
The Complete Guide to Building Strings in C++
- signa11 9y agocheap jabs at the venerable printf(...) notwithstanding, this article is ok. however, i must confess, string formatting still sucks in plain C++, and snprintf(...) is not too shabby.
- reality_czech 9y agoThe cheap jabs are all incorrect, as well. Any compiler from the last 10 years will warn you if you don't match your printf format string to the arguments. printf also supports reordering arguments using the %m$ syntax (read the man page). And yes, you can extend printf with your own types. A better guide to using strings in C++ is here: https://yosefk.com/c++fqa/io.html https://yosefk.com/c++fqa/io.html
- nikbackm 9y agoDoes the compiler also warn when you don't use a literal format string?
- aldanor 9y agoI’m surprised at how the author fails to mention fmtlib [1] with near-printf-like performance and sane (Python .format-like) formatting syntax, which also happens to generate much smaller binaries than Boost.Format. [1] https://github.com/fmtlib/fmt https://github.com/fmtlib/fmt
- gumby 9y agoAuthor doesn't have a comment section unfortunately, perhaps he reads this. Instead of the stream iterator, use a stream joiner and not deal with the crummy trailing delimiter. Also to_chars offers better formatting than to_string.
- gumby 9y agoto late to edit: there IS a comment section but my ad blocking had blocked it -- and someone else pointed out the stream joiners.
- Koshkin 9y agoInformative article. In practice, using ostringstream is the most useful technique.