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

c++, string

Solution

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

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

Problem

I need to copy a file into a string. I need someway to preallocate memory for that string object and a way to directly read the file content into that string's memory?

Original source

Related problems