Answer the question
In order to leave comments, you need to log in
How to remove duplicate words in lines using lines?
The task is to read the lines from the file, sort them alphabetically and remove duplicates, I managed to sort, but I don’t understand how to remove the repetitions.
void task3() {
ifstream slovar;
slovar.open("slovar.txt");
ofstream out;
out.open("output.txt");
list<string> lines;
while (!slovar.eof())
{
string s1;
getline(slovar, s1, '\n');
lines.push_back(s1);
}
lines.sort();
//lines.erase(unique(lines.begin(), lines.end()), lines.end());
copy(lines.begin(), lines.end(), ostream_iterator<string>(out, "\n"));
slovar.close();
out.close();
}
Answer the question
In order to leave comments, you need to log in
//lines.erase(unique(lines.begin(), lines.end()), lines.end());
auto in = ifstream("in.txt");
auto out = ofstream("out.txt");
list<string> ls;
copy(istream_iterator<string>(in), {}, back_inserter(ls));
ls.sort();
ls.unique();
copy(ls.begin(), ls.end(), ostream_iterator<string>(out, "\n"));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question