how to pre-allocate memory for a std::string object?

std::string has a .reserve method for pre-allocation.

std::string s;
s.reserve(1048576); // reserve 1 MB
read_file_into(s);

Leave a Comment