3 ms·
> The C++ equivalent might be snprintf There's std::format from C++20 too[1]. One can use std::ostringstream instead, I suppose, but at that point I'd probabl
by maleldil 5y ago
> The C++ equivalent might be snprintf
There's std::format from C++20 too[1]. One can use std::ostringstream instead, I suppose, but at that point I'd probably use snprintf too.
[1] https://en.cppreference.com/w/cpp/utility/format/format https://en.cppreference.com/w/cpp/utility/format/format
- spacechild1 5y agostd::ostringstream will certainly allocate its buffer on the heap. When you call the str() method to retrieve the final result, it will have to make another copy. So you will always have an extra allocation. Note that std::format still returns a std::string (which might allocate memory, depending on the size of the string), but that doesn't matter if it is going to be moved anyways. If you want to write into an existing buffer, you can use std::format_to_n as a modern replacement for snprintf.