Hi,I have an assignment that asks me to sort the words of a text (ascending) without duplicates being included. I manage to sort the words, but I just don't understand how to remove the duplicate words after being sorted. Here's my current progress :
// load a string vector with words from a whitespace
// separated text file and sort the words
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
vector<string> sV;
ifstream in("paragraph.txt");
string word;
// separates words in file at a whitespace
// and loads a string vector
while(in >> word)
sV.push_back(word);
cout << "Words before sorted:" << endl << endl;
for(int i = 0; i < sV.size(); i++)
cout << sV[i] << endl;
cout << "---------------" << endl;
cout << "Words after sorted:" << endl << endl;
sort( sV.begin(), sV.end() );
int i;
int j;
string y;
string z;
for(int i = 0; i < sV.size(); i++){
y = sV[i]; z=sV[i+1];
if (y==z){
j=i+1;
sV.erase (sV.begin()+j);
}
cout << sV[i] << endl;}
cin.get();
return 0;
}
Can someone help me to erase the duplicate words ? The text is a paragraph. I'm screwed.... @_@