Problem in erasing duplicate words from a vector.

I am trying to erase all the duplicate words from a vector. I found a function unique(); I wrote a program using this function but it doesn't work as I wanted.

How to do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>

using namespace std;

int main()
{
    vector<string>v={"sad","sad","sad","sad","fine","good","well","sad","sad","fine","good","good","sad","well"};

    v.erase(unique(v.begin(), v.end()),v.end());

    for(int i=0; i<v.size();i++)
            cout<<v[i]<<endl;

    return 0;
}


It's Output:
sad
fine
good
well
sad
fine
good
sad
well

But I want the output to be:

sad
fine
good
well
std::unique only works if the elements are sorted. If you don't mind that the elements get sorted you can use std::sort to sort the vector before using std::unique.
Thank you..it works! :)
Topic archived. No new replies allowed.