Deleting duplicate words from a line

I am trying to write a program that takes a line as input, finds the duplicate words and erase the duplicate words. Finally it will print a line where there are no duplicate words.

For example:
Input: what what what should i i say say
Output: what should i say

My code is given below, it's not working-

To to correct the program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);

    string s;
    getline(cin,s);

    istringstream is(s);

    vector<string> v;

    string word;

    while (is>>word)
        v.push_back(word);

    for(int i=0; i<v.size(); i++)
    {
        for(int j=1; j<v.size(); j++)
        {
            if(v[i]==v[j])
            {
                v.erase(v.begin()+j);
            }

        }
    }

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

    return 0;
}
Check if i equals to j, too.
When you erase an item, also decrement j, otherwise one of the "what" will be skipped.
Performance tip: init j with i+1, since all previous items have already been checked.
Topic archived. No new replies allowed.