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.... @_@

Recommended Answers

All 8 Replies

try this:

for(int i=0, size=sV.size(); i<size; i++)
{
     for(int j=0; j<size; j++)
     {
          if(sV[i].compare(sV[j]) == 0)
          {
               sV.erase(sV[j]);
          }
     }
}

it still doesn't work...

my bad.. i forgot that erase() changes the size of the vector. this is actually one time where continuous checking of size(), although not optimal, is actually a good thing:

for(int i=0; i<sV.size(); i++)
{
     for(int j=0; j<sV.size(); j++)
     {
          if(sV[i].compare(sV[j]) == 0)
          {
               sV.erase(sV[j]);
          }
     }
}

After trying your code, my code become like this :

// 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 << "Unsorted words:" << endl;
  for(int i = 0; i < sV.size(); i++)
    cout << sV[i] << endl;

  cout << "---------------" << endl;

  cout << "Sorted words:" << endl;
  sort( sV.begin(), sV.end() );

  int i;
  int j;
  for(int i = 0; i < sV.size(); i++)
  {
     for(int j=0; j<sV.size(); j++)
     {
          if(sV[i].compare(sV[j]) == 0)
          {
               sV.erase(sV[j]);
          }
     }
}
    cout << sV[j] << endl;

  cin.get();
  return 0;
}

But there is still an error that says no matching function in this part :

sV.erase(sV[j]);

And I don't know why ? Can you help me please?

The erase() function requires an iterator type argument, but i was hoping to get away with an array referrence.

Just out of curiosity, see if this works:

sV.erase(&sV[j]);

If not, this will work:

sV.erase(sV.begin() + j);

Thank you, the program started to work again! But once the programs enter the sorted words part, it's terminated -_-", here's the code again :

// 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 << "Unsorted words:" << endl;
  for(int i = 0; i < sV.size(); i++)
    cout << sV[i] << endl;

  cout << "---------------" << endl;

  cout << "Sorted words:" << endl;
  sort( sV.begin(), sV.end() );

  int i;
  int j;
  for(int i = 0; i < sV.size(); i++)
  {
     for(int j=0; j<sV.size(); j++)
     {
          if(sV[i].compare(sV[j]) == 0)
          {
               sV.erase(sV.begin() + j);
          }
     }
}
    cout << sV[j] << endl;

  cin.get();
  return 0;
}

sorry double post

the sort( ) function by default performs a strict < comparison between elements. Unfortunately, you can't compare strings with the < operator. Luckily, there is an overloaded version of sort( ) that will accept a user-defined comparison function. You can design a function that will return true if string1 < string2:

//This function will be used to perform an "alphabetical order comparison" between strings

bool string_compare(string word1, string word2)
{   
     #include<cctype>
          
     int size = 0;

     //determine shortest word length
     if(word1.size() < word2.size())
     {
          size = word1.size();
     }
     else
     {
          size = word2.size();
     }
 
     //perform char-by-char comparison
     //return true if words are in alphabetical order
     for(int i=0; i<size; i++)
     {
          if(toupper(word1[i]) < toupper(word2[i]))
          {
               return true;
          }
          else if(toupper(word[i]) > toupper(word2[i]))
          {
               return false;
          }
     }

     //handle two matching words, different length
     //example:  work and working
     if(word1.size() < word2.size())
     {
          return true;
     }
     else
     {
          return false;
     }
}

Now you can use sort( ) to compare strings:

#include<algorithm>

//sort strings to alphebetical order
sort(sV.begin(), sV.end(), string_compare);

Be sure to read up on how to use sort: http://www.cplusplus.com/reference/algorithm/sort/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.