How do I erase items of a vector?

Hallo,

my problem is the following:
I've created a list of game titles in a vector.
Now I want that the user chooses which one to erase.
And here is the problem. How do I do it?

The line with the problem is the line, in which I try to call the erase function.

Here's the code I have so far:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
cout << "Creating a list of game titles:\n";

vector<string> gameTitle;
vector<string>::const_iterator iter;

gameTitle.push_back("twoworlds");
gameTitle.push_back("gothic");
gameTitle.push_back("oblivion");
gameTitle.push_back("skyrim");
gameTitle.push_back("torchlight");

cout << "My game titles are:\n";
for (iter = gameTitle.begin(); iter != gameTitle.end(); ++iter)
{
cout << *iter << endl;
}

cout << "\nFinding a title.";
string title;

cout << "\nEnter a title to find:\n";
cin >> title;
iter = find(gameTitle.begin(), gameTitle.end(), title);
if (iter != gameTitle.end())
{
cout << "Title found.\n";
}
else
{
cout << "Title not found.\n";
}

cout << "\Erasing a title.\n";
cin >> title;
gameTitle.erase(gameTitle.begin(), title);
cout << "\nYour titles are:\n";
for (iter = gameTitle.begin(); iter != gameTitle.end(); ++iter)
{
cout << *iter << endl;
}

return 0;
}
Use the already found iter for erasing:

http://cplusplus.com/reference/stl/vector/erase/


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
gameTitle.erase( find( gameTitle.begin(), gameTitle.end(), title ) );
your problem may occured in: gameTitle.erase(gameTitle.begin(), title);
you can use erase operation only in this two modes: C.erase(p) or C.erase(b , e), p,b,e are all iterator.
so yuo must find the title and put the return value into an iterator,then erase it.
just like:

cout << "\Erasing a title.\n";
cin >> title;
iter = find(gameTitle.begin(), gameTitle.end(), title);
gameTitle.erase(iter);
Last edited on
Read this first: Erase-remove idiom
http://en.wikipedia.org/wiki/Erase-remove_idiom
Hallo,

thank you for all the answers and help. Problem solved!
Topic archived. No new replies allowed.