|
|
|
|
|
by OskarS
1967 days ago
|
|
It's blows my mind that something like this hasn't been in the standard library for, like, 20 years. Like, why can't i do this? std::string str = "Values: ";
for (auto &[key, val]: dict) {
str.append("%s: %d, ", key, value);
}
This is so much nicer to use than stringstream, and it's also so much more efficient! You could allocate a "big enough" string at the beginning, and then you would have no more allocations. You could also reuse the string, so you can avoid almost all allocations for the lifetime of the program. This is basically impossible to do with stringstream: it's just insane about copies and allocations, and it's more or less impossible to reuse buffers.The C++ format strings are an improvement, but just this simple thing (a super straight-forward translation of printf to C++ std::strings) is basically all you would ever need. C more or less got it right in the 70s, and then C++ spent decades with its own (significantly worse) solution. |
|