#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Phone
{
private:
string name;
string num;
public:
Phone(string n , string nm){
name = n;
num = nm;
}
string getName(){
return name;
}
string getNum(){
return num;
}
void setNum(string mynum){
num = mynum;
}
};//注意分号!
vector<Phone*> phones;
void add(){
string phone1;
//Add
while (true)
{
cin>>phone1;
if(!phone1.compare("Q"))
break;
string name = phone1.substr(0 ,phone1.find_first_of(","));
string num = phone1.substr(phone1.find_first_of(",")+1, phone1.length());
Phone* a = new Phone(name,num);
phones.push_back(a);
}
}
void read(){
//Read
for (vector<Phone*>::size_type j = 0; j != phones.size(); ++j)
{
Phone* phone = phones[j];
cout <<"Reading: "<< phone->getName()<<" Num: "<<phone->getNum()<<endl;
}
}
void read_iterator(){
//使用iterator
for(vector<Phone*>::iterator itr1 = phones.begin();itr1!=phones.end();++itr1){
Phone*phone=*itr1;
cout<<"Reading:"<<phone->getName()<<"Num:"<<phone->getNum()<<endl;
}
}
void mydelete(){
//Delete
cout<<"input what you want delete: ";
string removeword;
cin>>removeword;
vector<Phone*>::iterator itr = phones.begin();
while (itr != phones.end())
{
if (!removeword.compare((*itr)->getName())){ //如果两个字符串相等, compare将返回0
cout << "Deleting: "<< (*itr)->getName()<<endl;
itr = phones.erase(itr); //注意如何删除!
}else
{
++itr;
}
}}
void modify(){ //modify
vector<Phone*>::iterator itr1 = phones.begin();
while (itr1 != phones.end())
{
string modifyword = "gb";
if (!modifyword.compare((*itr1)->getName())){
cout << "Modifying: "<< (*itr1)->getName()<<endl;
(*itr1)->setNum("0000000");}
++itr1;
}}
int main()
{
add();
read();
mydelete();
modify();
cout<<"After motifying use iterator read: " <<endl;
read_iterator();
return 0;
}