Erase a string in a vector?

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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
void list_all();
string add_game(), rem_game();
int choice;
vector<string> game_list;
vector<string>::iterator the_iterator;

int main()
{
    cout << "press 1 to list games" << endl;
    cout << "press 2 to add a game" << endl;
    cout << "press 3 to remove a game" << endl;
    cout << "press 4 to exit" << endl;
    cin >> choice;

        switch(choice)
        {
        case 1:
            list_all();
            break;
        case 2:
            add_game();
            break;
        case 3:
            rem_game();
            break;
        case 4:
            return 0;
        default:
            cout << "not a valid option" << endl;
            main();
        }
}

void list_all()
{
    int list_numbers = game_list.size();
    cout << "here's a list of your titles : " << endl;
    for (the_iterator = game_list.begin(); the_iterator != game_list.end(); the_iterator++)
    {
        cout << list_numbers << " " << *the_iterator << endl;
    }
    main();
}

string add_game()
{
string game_title;
    cout << "enter game title : ";
    cin.ignore(); //ignores the whitespace between strings
    getline(cin, game_title);
    game_list.push_back(game_title);
    sort(game_list.begin(), game_list.end());
    main();
}

string rem_game()
{
string game_title;
    cout << "enter game to remove : " << endl;
    cin.ignore();
    getline(cin, game_title);
    game_list.erase(game_title); <<<< THIS DOES NOT WORK

    sort(game_list.begin(), game_list.end());
    main();
}

here is the error code :
no matching function for call to 'std::vector<std::basic_string <char>> :: erase (std::string&)
Last edited on
closed account (3qX21hU5)
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

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

Here is something you can do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace 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.

http://www.cplusplus.com/reference/algorithm/find/?kw=find
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.
Vectors aren't that slick unfortunately. You will need to loop through the vector and find what position contains the string you want to erase.

You should probably just create a function so you can call it when you need it.


1
2
3
4
5
6
7
8
9
10
11
12
13
void erase(std::vector<string>& v, string str)
{
        std::vector<string>::iterator iter = v.begin();

         while (iter != v.end())
        {
                if(*iter == str)
                      iter = v.erase(iter);
                else
                      iter++;
        }

}



There are better solutions out there. Just google them.
Last edited on
closed account (3qX21hU5)
You should probably just create a function so you can call it when you need it.


Sigh no one ever takes the time to learn the useful features of the algorithm header anymore :(. I really need to finish this article ;p
the usual approach is erase-remove: v.erase( remove(v.begin(), v.end(), str), v.end());
see http://en.wikibooks.org/wiki/More_C++_Idioms/Erase-Remove for example, or Effective STL
closed account (3qX21hU5)
Ohh even better thank you Cubbi I didn't think of that.
Thanks a lot guys, that was great :)
Topic archived. No new replies allowed.