关于c ++:函数中的向量,如何根据用户输入(字符串)创建用于删除索引的查找和擦除功能?

Vectors in functions, how to create find and erase function for deleting indexes based on user input (string)?

本问题已经有最佳答案,请猛点这里访问。

/*因此,老师为一个建立和修改电话簿的班级建立了主要和原型。现在,我几乎没有定义类函数,但是在定义他的一个原型"void erase"和"void find"时遇到了麻烦。我从void erase开始,它应该在矢量电话簿中找到一个人并将其删除,"void find"只是定位所需字符串的索引(被搜索者的姓名)。我已经列出了老师建造的主要部分,他给我们的原型,以及到目前为止我在定义它们方面所做的工作。您可以在原型和我的工作中找到我询问的两个函数。请注意,我不允许对教师的主要或原型进行任何更改。*/

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
**the main:**

    #include <iostream>
    #include <string>
    #include <vector>
    #include <time.h>
    #include"phonebook.h"

    using namespace std;

    int main()
    {

        vector<Person> phone_book;
        string name;
        int number;
        int answer;

        srand((int)(time(0)));

        phone_book.push_back(Person("Bruin, Joe", 5556456));
        phone_book.push_back(Person("Simpson, Homer", 5557471));
        phone_book.push_back(Person("Duffman, Barry", 5533331));


        cout <<"
"
;
        cout <<"Your phone book contains the following names and numbers:
"
;

        for (int i=0; i < phone_book.size(); i++)
        {

            phone_book[i].print();
            cout <<"
"
;
        }
        cout <<"
"
;
        answer=0;

        while (answer != 8)
        {
            cout <<"
Choose from the following options:

"
;

            cout <<"1) Add people to the phone book.
"
;
            cout <<"2) Erase a person from the phone book.
"
;
            cout <<"3) Sort the phone book.
"
;
            cout <<"4) Shuffle the phone book.
"
;
            cout <<"5) Reverse the phone book.
"
;
            cout <<"6) Print the phone book.
"
;
            cout <<"7) Look up a person in the phone book.
"
;
            cout <<"8) Quit.

"
;

            cin >> answer;

            string clear;

            getline(cin, clear);

            if (answer == 1)
                add_people(phone_book);
            else if (answer == 2)
            {
                cout <<"Enter a name:";
                getline(cin, name);
                erase(phone_book, name);

            }
            else if (answer == 3)
                sort(phone_book);
            else if (answer == 4)
                shuffle (phone_book);
            else if (answer == 5)
                reverse(phone_book);
            else if (answer == 6)
            {
                cout <<"
"
;
                cout <<"Your phone book contains the following names and numbers:
"
;
                print(phone_book);
            }
            else if (answer ==7)
            {                  
                cout <<"Enter a name:";
                getline(cin, name);
                int number = lookup(phone_book, name);
                if (number > 0)
                {
                    cout <<"

The number for"
<< name <<" is:" << number <<"

"
;
                }
                else
                    cout << name <<" not found in the phone book.
"
;
            }
        }
        return 0;
    }

**the prototypes:**

    #ifndef PHONEBOOK_H
    #define PHONEBOOK_H
    #include <string>
    #include <vector>
    using namespace std;

    class Person
    {
    public:
        Person();
        Person(string new_name, int new_phone);
        string get_name() const;
        int get_phone() const;
        bool operator < (Person p) const;
        void print() const;

    private:
        string name;
        int phone;
    };

    void add_people(vector<Person> &phone_book);
    void erase(vector<Person> &phone_book, string name);
    void sort(vector<Person> &phone_book);
    void shuffle(vector<Person> &phone_book);
    void reverse(vector<Person> &phone_book);
    void print(vector<Person> &phone_book);
    int lookup(const vector<Person> &phone_book, string name);
    #endif

**my work so far:**

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include"phonebook.h"

Person::Person()
{
    name ="NONE";
    phone = 0000000;

}
Person::Person(string new_name, int new_phone)
{
    name=new_name;
    phone=new_phone;
}
string Person::get_name() const
{
    return name;
}
int Person::get_phone() const
{
    return phone;
}
bool Person::operator < (Person p) const
{
    return name < p.name;
}
void Person::print() const
{
    cout << endl << name <<"" << phone;
}

void add_people(vector<Person> &phone_book)
{
    cout <<"Please enter the new name:";
    string s;
    getline(cin, s);
    cout <<"Please enter new number:";
    int number;
    cin >> number;
    phone_book.push_back(Person(s,number));
}
void erase(vector<Person> &phone_book, string name)
{
    for(int i=0; i <= phone_book.size()-1; i++)
    {
        phone_book[i] = phone_book[i+1];
    }
}
void sort(vector<Person> &phone_book)
{
}
void shuffle(vector<Person> &phone_book)
{
}
void reverse(vector<Person> &phone_book)
{
}
void print(vector<Person> &phone_book)
{

}
int lookup(const vector<Person> &phone_book, string name)
{

}


lookup开始比较容易,因为可以在erase中重用它。

您需要做的是将电话簿中的每个姓名与您要查找的姓名进行比较。