vector problem with erase()

This is a discussion on vector problem with erase() within the C++ Programming forums, part of the General Programming Boards category; ok im reading a c++ book called beginning c++ game programming and doing one of the exercises in it which ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    vector problem with erase()

    ok im reading a c++ book called beginning c++ game programming and doing one of the exercises in it which is:

    write a program using vectors and iterators that allows a user to maintain a list of his or her favorite games. The program should allow the user to list all game titles, add a game title, and remove a game title.

    I finished the code and upon running everything seems to work except one thing. When i try to remove the last game in the list it freezes and has to be ended. Ive tested everything else and that seems to be the only problem and I have no idea why, so if someone could help me I would be very grateful. Don't know if it matters, but im using Dev-C++.

    Code:
    #include <vector>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        vector<string> favGames;
        
        vector<string> choices;
        choices.push_back("add");
        choices.push_back("remove");
        choices.push_back("end");
        
        vector<string>::const_iterator iter;
        
        string gameTitle;
        string choice;
        
        bool cont = true;
        
        cout << "Use this program to make and modify a list of your favorite games.\n";
        
        while(cont==true)
        {
             if(favGames.empty())
                  cout << "\nYour list is currently empty.\n";
             else // displays list
             {     
                   cout << "\nFavorite Game list:\n";
                   for(iter = favGames.begin(); iter != favGames.end(); ++iter)
                       cout << *iter << endl;
             }
             
             do
             {
                  cout << "\nadd remove or end? ";
                  cin >> choice;
             
                  if(choice == choices[0])
                  {
                       cout << "Whats the name of the game to add?\n";
                       cin  >> gameTitle;
                       favGames.push_back(gameTitle);
                       break;
                  }
                  else if(choice == choices[1])
                  {
                       if(favGames.empty())
                       {
                            cout << "Your list is already empty.\n";
                            break;
                       }
                       cout << "Whats the name of the game to remove?\n";
                       cin  >> gameTitle;
                       int pos = 0;
                       int oldSize = favGames.size();
                       for(iter = favGames.begin(); iter != favGames.end(); ++iter)
                       {
                            if(gameTitle == *iter)
                                 favGames.erase(favGames.begin()+ pos);
                            ++pos;
                       }
                       if(oldSize == favGames.size())
                            cout << "No such game in the list.\n";
                       break;
                  }
                  else if(choice == choices[2])
                  {
                       cout << "Goodbye.\n";
                       cont = false;
                       break;
                  }
                  else
                  {
                       cout << "Please choose valid choice";
                  }
             }while(true);
        }
        system("PAUSE");
        return 0;
    }

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    First, you should not be using both pos and iter. Use one or the other.

    The problem is, erase() invalidates your iterator. Do this instead:

    Code:
                       for(iter = favGames.begin(); iter != favGames.end(); ) // Note: NO ++iter here!
                       {
                            if(gameTitle == *iter)
                                 iter = favGames.erase(iter);
                            else ++iter;
                       }
    Erase deletes the element and gives an iterator to the next element in the vector. If you choose not to erase the element, then you move to the next one. Note: This is true of all .erase() EXCEPT std::set, std::map, std::multiset, and std::multimap (the unordered containers).

    There is no need for pos at all; an iterator is a position.
    Last edited by Cat; 10-03-2006 at 07:44 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    thanks for the help

    oh and one more thing, right now I can only enter a game title one word long, how do you enter whole sentences using cin?

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    getline(cin, gameTitle);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    thanks again for the quick reply

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    'Remove/Erase Idiom'

    Code:
                favGames.erase(remove(faveGames.begin(), faveGames.end()), faveGames.end());
    http://www.roguewave.com/support/doc...ef/remove.html
    http://code.box.sk/newsread.php?newsid=773
    http://www.codeguru.com/forum/showthread.php?t=231045

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about erase()
    By Sharke in forum C++ Programming
    Replies: 8
    Last Post: 06-10-2009, 02:22 AM
  2. Question about using erase() with lists (STL)
    By apacz in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2005, 11:51 AM
  3. string erase function problems
    By westm2000 in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2003, 11:19 PM
  4. erase in vector
    By stimpyzu in forum C++ Programming
    Replies: 18
    Last Post: 11-23-2002, 01:23 AM
  5. will not erase this history for internet explorer
    By HelpPLease123 in forum Tech Board
    Replies: 8
    Last Post: 11-02-2002, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21