在vector中放入结构体数据,对结构体数据进行管理。
//通讯录
#include<iostream>
#include<vector>
#include<string>
#include<windows.h>
using namespace std;
struct txl
{
string phone;
string name;
};
vector<txl> host;
txl new_lxr;
void insert_lxr()
{
cout<<"请输入号码:";
cin>>new_lxr.phone;
cout<<"请输入姓名:";
cin>>new_lxr.name;
host.push_back(new_lxr);
}
void dispaly()
{
vector<txl>::iterator pos;
system("cls");
cout<<"当前共有"<<host.size()<<"个联系人"<<endl;
cout<<endl;
for(pos=host.begin();pos!=host.end();++pos)
{
cout<<pos->phone<<" "<<pos->name<<endl;
}
cout<<endl;
}
void delete_lxr()
{
if(host.empty())
{
cout<<"通讯录为空,请先加入!"<<endl;
return;
}
int i;
string i_phone,i_name;
vector<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;
for(pos=host.begin();pos!=host.end();++pos)
{
if(pos->phone==i_phone)
{
host.erase(pos);
cout<<"删除成功"<<endl;
return;
}
}
cout<<"没有找到此号码!"<<endl;
}
if(i==2)
{
cout<<"请输入姓名:";
cin>>i_name;
for(pos=host.begin();pos!=host.end();++pos)
{
if(pos->name==i_name)
{
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;
string i_phone,i_name;
vector<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;
for(pos=host.begin();pos!=host.end();++pos)
{
if(pos->phone==i_phone)
{
cout<<"找到"<<endl;
cout<<pos->phone<<":"<<pos->name<<endl;
return;
}
}
cout<<"没有找到此号码!"<<endl;
}
if(i==2)
{
cout<<"请输入姓名:";
cin>>i_name;
for(pos=host.begin();pos!=host.end();++pos)
{
if(pos->name==i_name)
{
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;
vector<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;
for(pos=host.begin();pos!=host.end();++pos)
{
if(pos->phone==i_phone)
{
cout<<"找到联系人 ";
cout<<pos->phone<<":"<<pos->name<<endl;
cout<<"请输入新号码:";
cin>>i_phone;
pos->phone=i_phone;
return;
}
}
cout<<"没有找到此号码!"<<endl;
}
if(i==2)
{
cout<<"请输入姓名:";
cin>>i_name;
for(pos=host.begin();pos!=host.end();++pos)
{
if(pos->name==i_name)
{
cout<<"找到联系人 ";
cout<<pos->name<<":"<<pos->phone<<endl;
cout<<"请输入新姓名:";
cin>>i_name;
pos->name=i_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;
}
运行结果: