Hacker News new | ask | show | jobs
by CJefferson 5368 days ago
Most C++03 std::strings (including g++'s) are copy on write, so inserting into a hash_map will not copy the string, only update some reference counts.

(Pedanticness: Assuming a few things, like you don't have any references or pointers into the string).

1 comments

Yes, it just so happens the STL I use switched from a ref counted imp to a "short string" optimized one where tiny strings are kept on the stack. Again, it was done for large scale tradeoffs in the app as a whole once memory usage was analyzed. So without the C++11 enhancements which allow any string imp to perform as best as possible, apps must be aware of how their particular STL works under the hood. (or at least my apps do :))