下面通讯录,因为使用set容器,里面通过set的成员函数find进行查找,使姓名查找只需对数复杂度,也有通过算法find_if进行查找,使号码查找则需要线性复杂度。
//高效通讯录 v1.0版本 -2015.1.28
#include<iostream>
#include<set>
#include<string>
#include<algorithm>
#include<windows.h>
using namespace std;
class txl
{
public:
txl(){}
txl(string iphone,string iname):phone(iphone),name(iname){}
~txl(){}
bool operator<(const txl& a)const//自定义set排序准则
{
return name<a.name;
}
string name;
string phone;
};
class txlAdapter:public unary_function<txl,bool>//仿函数,用来支持通过号码查找联系人
{
private:
string phone;
public:
explicit txlAdapter(string iphone):phone(iphone){}
bool operator()(const txl& b)
{
return (b.phone==phone);
}
};
multiset<txl> host;
txl new_lxr;
void insert_lxr()
{
cout<<"请输入号码:";
cin>>new_lxr.phone;
cout<<"请输入姓名:";
cin>>new_lxr.name;
host.insert(new_lxr);
}
void dispaly()
{
set<txl>::iterator pos;
system("cls");
cout<<"当前共有"<<host.size()<<"个联系人"<<endl;
cout<<endl;
for(pos=host.begin();pos!=host.end();++pos)
{
cout<<pos->name<<" "<<pos->phone<<endl;
}
cout<<endl;
}
void delete_lxr()
{
if(host.empty())
{
cout<<"通讯录为空,请先加入!"<<endl;
return;
}
int i;
txl tmp_phone,tmp_name;
string i_phone,i_name;
set<txl>::iterator pos;
cout<<"1.按照号码删除"<<endl;
cout<<"2.按照姓名删除"<<endl;
cout<<"3.全部删除"<<endl;
cout<<"0.退出删除"<<endl;
cin>>i;
if(i==0)
{
return;
}
if(i==1)
{
cout<<"请输入号码:";
cin>>i_phone;
pos=find_if(host.begin(),host.end(),txlAdapter(i_phone));
if(pos!=host.end())
{
host.erase(pos);
cout<<"删除成功"<<endl;
return;
}
cout<<"没有找到此号码!"<<endl;
}
if(i==2)
{
cout<<"请输入姓名:";
cin>>i_name;
tmp_name.name=i_name;
pos=host.find(tmp_name);
if(pos!=host.end())
{
host.erase(pos);
cout<<"删除成功"<<endl;
return;
}
cout<<"没有找到此联系人!"<<endl;
}
if(i==3)
{
host.erase(host.begin(),host.end());
cout<<"删除成功"<<endl;
return;
}
return;
}
void find_lxr()
{
if(host.empty())
{
cout<<"通讯录为空,请先加入!"<<endl;
return;
}
int i;
txl tmp_phone,tmp_name;
string i_phone,i_name;
set<txl>::iterator pos;
cout<<"1.按照号码查找"<<endl;
cout<<"2.按照姓名查找"<<endl;
cout<<"0.退出查找"<<endl;
cin>>i;
if(i==0)
{
return;
}
if(i==1)
{
cout<<"请输入号码:";
cin>>i_phone;
pos=find_if(host.begin(),host.end(),txlAdapter(i_phone));
if(pos!=host.end())
{
cout<<"找到"<<endl;
cout<<pos->phone<<":"<<pos->name<<endl;
return;
}
cout<<"没有找到此号码!"<<endl;
}
if(i==2)
{
cout<<"请输入姓名:";
cin>>i_name;
tmp_name.name=i_name;
pos=host.find(tmp_name);
if(pos!=host.end())
{
cout<<"找到"<<endl;
cout<<pos->name<<":"<<pos->phone<<endl;
return;
}
cout<<"没有找到此联系人!"<<endl;
}
return;
}
void change_lxr()
{
if(host.empty())
{
cout<<"通讯录为空,请先加入!"<<endl;
return;
}
int i;
string i_phone,i_name;
txl tmp_name,tmp_phone;
set<txl>::iterator pos;
cout<<"1.修改号码"<<endl;
cout<<"2.修改姓名"<<endl;
cout<<"0.退出查找"<<endl;
cin>>i;
if(i==0)
{
return;
}
if(i==1)
{
cout<<"请输入原号码:";
cin>>i_phone;
pos=find_if(host.begin(),host.end(),txlAdapter(i_phone));
if(pos!=host.end())
{
cout<<"找到联系人 ";
cout<<pos->phone<<":"<<pos->name<<endl;
cout<<"请输入新号码:";
cin>>i_phone;
tmp_phone.name=pos->name;
tmp_phone.phone=i_phone;
host.erase(pos);
host.insert(tmp_phone);
return;
}
cout<<"没有找到此号码!"<<endl;
}
if(i==2)
{
cout<<"请输入原姓名:";
cin>>i_name;
tmp_name.name=i_name;
pos=host.find(tmp_name);
if(pos!=host.end())
{
cout<<"找到联系人 ";
cout<<pos->name<<":"<<pos->phone<<endl;
cout<<"请输入新姓名:";
cin>>i_name;
tmp_name.name=i_name;
tmp_name.phone=pos->phone;
host.erase(pos);
host.insert(tmp_name);
return;
}
cout<<"没有找到此联系人!"<<endl;
}
return;
}
int main()
{
cout<<"联系人为空,请加入一个联系人"<<endl;
insert_lxr();
dispaly();
int i;
while(1)
{
dispaly();
cout<<"1.加入联系人"<<endl;
cout<<"2.查找联系人"<<endl;
cout<<"3.删除联系人"<<endl;
cout<<"4.修改联系人"<<endl;
cout<<"0.退出"<<endl;
cin>>i;
if(i==0)
break;
switch(i)
{
case 1:insert_lxr();continue;
case 2:find_lxr();Sleep(1000);;continue;
case 3:delete_lxr();Sleep(1000);;continue;
case 4:change_lxr();Sleep(1000);continue;
default:
cout<<"输入错误!"<<endl;
Sleep(500);
continue;
}
}
system("pause");
return 0;
}