question about the erase() member function

I am writing a small program that will allow a user to enter video game titles, list them, and remove specific titles. Here is the code:

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    int numTitles = 0;
    vector<string>::iterator finder;
    vector<string>::const_iterator iter;
    
    vector<string> titles(numTitles);
    
    cout << "\t\tWelcome!\n\n";
    
    string choice;
    
    while(choice != "quit")
    {
        cout << "\n\nWould you like to list your favorite games (type 'list')\n" << endl;
        cout << "add more titles to your list (type 'add')\n" << endl;
        cout << "or remove any titles from your list? (type 'remove')\n\n";
        
        cin >> choice;
        
        if (choice == "add")
        {
                ++numTitles;
                string addTitle;
                cout << "\n\nEnter the name of the title you wish to add to the list: ";
                cin >> addTitle;
                titles.push_back(addTitle);
                cout << "\nThe title " << addTitle << " was added to the list.\n";
                }
                
        if (choice == "list")
        {
                if (numTitles > 0)
                {
                                cout << "\nYour favorite games are: \n\n";
                                for (iter = titles.begin(); iter != titles.end(); ++iter)
                                                                cout << *iter << endl;
                }
        else
        cout << "\n\nYou have no titles listed.\n\n";
        }
        
        if (choice == "remove")
        {
                string removeTitle;
                string remover;
                cout << "\nWhich title would you like to remove?\n";
                cin >>  removeTitle;
                
                for (finder = titles.begin(); *finder != removeTitle; finder++)
                                remover = *finder;
                
                titles.erase(titles(remover));
                --numTitles;
                
                cout << "\nThe title " << removeTitle << " has been removed.\n";
        }
        
        }
    return 0;
}
        


The part shown in bold is what I have a question about. I realize I'm not using the erase() function correctly, that is, because the book I'm learning from does not get into detail about it. What I'm trying to do is cycle through the list created by the user until the word specified (by the user) is equal to a word (added to the list by the user) in the list. Once the iterator is equal to the word specified, the program should delete the word from the vector. If anyone could help me with a correct way of doing this that would be awesome.

I apologize if this is sloppy, horribly incorrect, or too difficult to understand. I'm new to programming so any criticism, tips, etc. on anything else would be great. Thanks.
erase() takes an iterator. The iterator you have is finder. It is most easily accomplished by
removing the element in the for() loop and breaking.

1
2
3
4
5
6
for( finder = titles.begin(); finder != titles.end(); ++finder )
    if( *finder == removeTitle )
    {
        titles.erase( finder );
        break;
    }


However, you would also benefit by looking up std::find(); it will do most of the above
for you.

1
2
3
finder = std::find( titles.begin(), titles.end(), removeTitle );
if( finder != titles.end() )
    titles.erase( finder );
Topic archived. No new replies allowed.