Hey guys, I have to make a little program that makes a list of the users favourite games. It has to be able to display the list, add to the list, and delete from the list. It has to use vectors and iterators too. I've done everything except I cant make it delete a string from the vector, the problem is with line 68, I don't see why it wont work...seems legit :)
Its because you are passing game_list.erase() a string. It doesn't accept a string it accepts a iterator(pointer) to the element you want to erase. Here is the documentation
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
usingnamespace std;
int main ()
{
vector<string> game_list = {"Tomb Raider", "Minecraft", "Starcraft"};
string game;
cout << "Enter game you want to delete: ";
cin >> game;
// Uses the find() function to return a iterator pointing to the game we want
// to delete.
vector<string>::iterator result = find(game_list.begin(), game_list.end(), game);
// If result points to game_list.end() it couldn't find the game so
// we can't delete it. If it isn't it delete the game.
if (result == game_list.end())
cout << "That game is not in there!" << endl;
else
game_list.erase(result);
// You can ignore this it is just printing the vector
for (string x : game_list)
cout << x << endl;
return 0;
}
Use the find() function to get a iterator so you can then pass it to erase() and delete the game from the vector.
This won't work because the data you're passing to the erase function is a string, erase requires an iterator. So you would use the begin() function and off set it by the position in the vector of the element you are trying to erase. There is an example of how to do this here: http://www.cplusplus.com/reference/vector/vector/erase/ it's up to you to determine where in the vector the element is.