这两天学习有点乱,找不到重心。下面就分享下通讯录更新后的版本吧!
#include<bits/stdc++.h>
using namespace std;
class Phone
{
string name,tel;
public:
Phone()
{
name="li";
tel="123456789";
}
Phone(string na,string te):name(na),tel(te){}
string getname(){return name;}
string gettel(){return tel;}
void xiugai(string a,string b)
{
name=a;
tel=b;
}
void pri()
{
cout<<name<<" "<<tel<<endl;
}
};
class Setphone
{
vector<Phone>all;
multimap<string,string>find1;
public:
void addphone();
void fixphone();
void cutphone();
void readphone();
void writephone();
void chaname();
};
void Setphone::addphone()
{
string na,te;
cin>>na>>te;
find1.insert(make_pair(na,te));
}
void Setphone::chaname()
{
multimap<string,string>::iterator it2;
string na;
cin>>na;
for(it2=find1.lower_bound(na);it2!=find1.upper_bound(na);it2++)
{
cout<<it2->first<<" "<<it2->second<<endl;
}
}
void Setphone::cutphone()
{
multimap<string,string>::iterator it2;
string na;
cin>>na;
it2=find1.find(na);
if(it2!=find1.end())
find1.erase(na);
}
void Setphone::fixphone()
{
multimap<string,string>::iterator it2;
string na,newphone1;
cin>>na>>newphone1;
it2=find1.find(na);
if(it2!=find1.end())
it2->second=newphone1;
}
void Setphone::readphone()
{
ifstream in;
in.open("C:\\GameDownload\\student.txt");
while(!in.eof())
{
string one;
string nams,tels;
getline(in,one);
if(one.empty())
continue;
istringstream is(one);
is>>nams;
is>>tels;
find1.insert(make_pair(nams,tels));
}
in.close();
}
void Setphone::writephone()
{
ofstream out;
out.open("C:\\GameDownload\\student.txt");
all.clear();
for(auto it1=find1.begin();it1!=find1.end();it1++)
{
Phone me;
me.xiugai(it1->first,it1->second);
all.push_back(me);
}
for(auto it1=all.begin();it1!=all.end();it1++)
{
out<<(*it1).getname()<<"\t"<<(*it1).gettel()<<endl;
}
out.close();
}
int main()
{
int n;
Setphone cao;
cao.readphone();
cin>>n;
while(n--)
{
cao.addphone();
}
cao.chaname();
cao.cutphone();
cao.chaname();
cao.fixphone();
cao.chaname();
cao.writephone();
return 0;
}