I know on StackOverflow there is a bunch of questions like that, but it is always a little different and although I cannot get it working. I'll display only lines which are responsible for sorting, with reading file and displaying all of data from it I hadn't any issues.
int fileLenght = 0;
string temp;
//counted lines to create a vector or array
while (getline(fin, temp)){
fileLenght++;
}
fin.seekg(0, ios::beg); //come back to the beginning
//vector<string> vec; // tried with vector
while (getline(fin, temp)){
//need to sort it here before displaying
//output += temp; //displays all of file without sorting
//output += "\n";
//vec.push_back(temp); //tried with vector
}
fin.close();
//cout << output << endl;
sort(vec.begin, vec.end);
tried to make with this, but compiler gives an error that *expected 3 arguments, but given 2*. If I add the function below as third parameter, so it says *expected 2 arguments, but given 3*.
for (vector<string>::const_iterator i = vec.begin(); i != vec.end(); ++i){
cout << *i << "\n" << endl;
}
And in one of answers I found this function which sorts 2 strings, but if I use it, I need to know position in future which I gonna insert it to.
bool compareNoCase(string first, string second)
{
int i = 0;
while ((i < first.length()) && (i < second.length()))
{
if (tolower(first[i]) < tolower(second[i])) return true;
else if (tolower(first[i]) > tolower(second[i])) return false;
i++;
}
if (first.length() < second.length()) return true;
else return false;
}
I don't think this is a good one in my case.
This is very simple I guess, but since in Java I do it easily, but in C++ I found it a little complicated. Please, how can I display each line from file sorted alphabetically? If need more info, just ask me. Thanks.
Aucun commentaire:
Enregistrer un commentaire