what would be the most efficient way to sort a file with millions of string inside it?
I know how to do this, if it were a file with millions if Integers in it.
how to modify our approach if it is string? or any new approach?
what would be the most efficient way to sort a file with millions of string inside it?
I know how to do this, if it were a file with millions if Integers in it.
how to modify our approach if it is string? or any new approach?
First load all the file in memory, then use std::sort
You can do something like this:
std::fstream MyFile("MyFile.txt",ios_base::in);
std::vector<std::string>MyStrings;
while(MyFile.eof()==0){
std::stringstream MyBuffer;
MyBuffer<<MyFile;
MyStrings.push_back(MyBuffer.str())
}
std::sort(MyStrings.begin(),MyStrings.end()); //no need for compare function because it exsits for std::string