Luckily it isn't required for merge sort.
Yes. To be exact, something like:
Code:
std::string temp;
temp.reserve( MAX_LINE_LEN );
std::vector< std::string > lines( NLINES, temp );
// or if std::string tries to be smart:
std::vector< std::string > lines( NLINES );
for( ... ) {
line->reserve( MAX_LINE_LEN );
}
Where MAX_LINE_LEN = 1024 and NLINES is around 256k. Remember that the point is to avoid reallocations and swapping.
This way you can read 256k lines into preallocated space, which should be fast enough. Then you can sort that vector using std::sort, dump everyting into a temporary file and read another set of lines. And so on until the end of file. When you are finished you can free the memory and continue with
merge sort algorithm.