U
U
ugrumovi2020-12-16 20:23:26
C++ / C#
ugrumovi, 2020-12-16 20:23:26

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

1 answer(s)
R
Roman, 2020-12-16
@ugrumovi

//lines.erase(unique(lines.begin(), lines.end()), lines.end());

^^^
Not that unique tried to use.
std::list has its own
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 question

Ask a Question

731 491 924 answers to any question