基本思想
1.增加
输入要增加的信息,直接用vector的push_back()
void Student::Add() {
cout << "please input the add information" << endl << endl;
Student s;//之前定义的学生类
s.input();
vec.push_back(s);
print();//一个输出函数
}
2.查找
查找可以分很多种,比如,按学号查找,姓名查找等等,但核心思想都是一样,这里只介绍用姓名查找。首先,输入要查找的学生姓名,用迭代器iterator进行遍历一遍,然后 进行判断是否有相同,若有相同,输出该学生信息,跳出循环,若循环走完都没有找到直接输出“查无此人”。
cout << "please input the find name" << endl << endl;
string na;
cin >> na;
int flag = 0;//用于标记是否找到
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
it->output();
flag = 1;
break;
}
}
if(!flag)
cout << "查无此人" << endl;
3.修改
建立在查找的基础上,只不过是在遍历判断之后,若相同的话,在进行一次信息的输入,对原来的信息进行覆盖即可。若不同直接输出“本无此人”。
cout << "please input the change name" << endl << endl;
string na;
cin >> na;
int flag = 0;
for (it = vec.begin(); it !=