Thread: Remove()

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Remove()

    Hello!

    Would there be an easier way to remove several characters than this?
    Code:
                            
       while ( file >> line ) {
              line.erase( remove(line.begin(),line.end(),'.') , line.end() );
              line.erase( remove(line.begin(),line.end(),',') , line.end() );
              line.erase( remove(line.begin(),line.end(),'!') , line.end() );
              vWords.push_back(line);
       }
    The for loop not working
    Code:
       while (file >> szWord) {
            for (int i = 0; szWord[i]; i++){
               if ( szWord[i] == '.' || szWord[i] == ',' || szWord[i] == '!'){
                    szWord [i] = ' ';
               }  
            }  
            vWords.push_back(szWord);
        }
    The whole code
    Code:
    int main ()
    {
      vector<string> vWords1;
      vector<string> vWords2;
      
      read_file("1.txt", vWords1);
      read_file("2.txt", vWords2);
      
      sort (vWords1.begin(), vWords1.end());
      sort (vWords2.begin(), vWords2.end());
      
      vector<string> v;
      
      set_intersection (vWords1.begin(), vWords1.end(),
                        vWords2.begin(), vWords2.end(), back_inserter(v));
      
      cout << "Intersection has " << v.size() << " elements.\n";
      for (size_t i = 0; i != v.size(); ++i)
        cout << "The "<< i+1 <<"." << " element is: " << v[i] << "\n";
      
      system("pause");
    }
      void read_file( const char* name, vector<string>& vWords)
      {
        ifstream file(name);
        string line;
        vWords.clear();
        while ( file >> line ) {
              line.erase( remove(line.begin(),line.end(),'.') , line.end() );
              line.erase( remove(line.begin(),line.end(),',') , line.end() );
              line.erase( remove(line.begin(),line.end(),'!') , line.end() );
              line.erase( remove(line.begin(),line.end(),'?') , line.end() );
             vWords.push_back(line);
        }
      }
    Last edited by Ducky; 01-12-2008 at 12:52 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is also remove_if. You'd write a function or a function object that - given a character as input - returns whether the character should be removed.

    The non-working for loop might work if you actually removed the spaces from the string.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks to you, Anon!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 12:55 PM
  2. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 01:46 PM
  3. randomly remove elements
    By joan in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2006, 12:22 PM
  4. Remove " character
    By Kirmie in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2005, 09:18 PM
  5. trying to remove item from an array
    By rugger78 in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2004, 10:26 AM