Jump to content
  • Advertisement
Sign in to follow this  
Trandafira

Iterators, Containers and deleting

This topic is 4325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think I am having a similar problem to Silvo but because i am such a noob I can't figure out how to adapt it to mine =_=. Anyway, this is a program that is storing names of "favorite games" as such in a container (vector) and listing them, then giving the user option to add one more or delete one. I've made the majority of it a case/switch statement, with the original being a do/while that lists options of what they can do. I've had two problems with it. When i try to delete a name the program crashes. I use the code: case 3: { cout << "\nEnter the game you wish to remove from the list\n"; cin >> delGame; for (iter = favGames.begin(); iter!= favGames.end(); ++iter) { if ( delGame == *iter ) favGames.erase(*iter); } The other problem I've had is that to end the sequence of games the user intially puts in you type the word end which takes it back to the do/while "menu" but i don't know how to make the word "end" not be seen as one of the games on the list. ( I tried to use a statement similar to the one shown above to search the container and if it found the string "end" in one it should delete it but obviously that won't work if the above statement doesn't) Thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
erase() invalidates the iterator you gave it, meaning that you can't use ++iter. Instead, use the return value from erase().

Share this post


Link to post
Share on other sites
So the erase() function deletes the iterator? How can i just delete what the iter is pointing to? (The actual value or string in this case) without invalidating the iterator?

Share this post


Link to post
Share on other sites
erase doesnt delete the iterator it deletes what the iterator is pointing to BUT not the iterator will be pointing to something that has been deleted and so is invalid but erase return a valid iterator that points to the spot you were up to

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!