#include<iostream>
#include <list>
#include <map>
#include <string>
#include <vector>
using namespace std;
static int i=0;
struct msg
{
msg(const char*,const char*,const char*);
string name;
string phone;
string addr;
};
msg::msg(const char* name,const char* phone,const char* addr)
:name(name),phone(phone),addr(addr){}
void init(map<char,vector<struct msg> >& phone)
{
char ch;
for(ch='A';ch<='Z';ch++)//创建27对键值对
{
phone[ch]=vector<struct msg>();//key分别为各字母,值为空数组容器
}
phone['#']=vector<struct msg>();
}
void show(map<char,vector<msg> >& phone)
{
map<char,vector<msg> >::iterator the;//phone的迭代器
vector<msg>::iterator va_the;//值的迭代器
for(the=phone.begin();the!=phone.end();the++)
{
va_the=the->second.begin();//the->second为数组名,加.begin()后才为指针
if(the->second.size()!=0)
{
cout<<the->first<<endl;
for(;va_the!=the->second.end();va_the++)
{
cout<<" "<<"姓名:"<<va_the->name\
<<" 电话:"<<va_the->phone\
<<" 地址:"<<va_the->addr<<endl;
}
}
}
}
char change(char ch)
{
if(ch>='A' && ch <='Z')//大写不变
return ch;
else
return ch-32;//小写转大写
}
bool insert(map<char,vector<msg> >& phone,struct msg data)
{
map<char,vector <msg> >::iterator the;
for(the=phone.begin();the!=phone.end();the++)
{
if(the->first==change(data.name[0]))//找匹配的首字母
{ //大小写转换
the->second.push_back(data);
return true;
}
}
the=phone.find('#');//没找到就放#目录下面
the->second.push_back(data);
return true;
}
bool find(map<char,vector<msg> >& phone,const char* name)
{
char* p=(char*)name;
map<char,vector<msg> >::iterator the;
the=phone.find(change(*p));//找到首字母所在目录//一定要注意大小写转换!!
vector<msg>::iterator va_the;
va_the=the->second.begin();
for(;va_the!=the->second.end();va_the++)
{
if(va_the->name==name)
{
cout<<"found "<<name<<endl;
return true;
}
}
cout<<" not found"<<endl;
// va_the=NULL; 迭代器不能指向空,只能于end()比较 类相关东西
return false;//找不到返回end,没有意义的值
}
bool mod(map<char,vector<msg> >& phone,const char* name)
{
if(!find(phone,name))
return false;
char* p=(char*)name;
map<char,vector<msg> >::iterator the;
the=phone.find(change(*p));//找到首字母所在目录//一定要注意大小写转换!!
vector<msg>::iterator va_the;
va_the=the->second.begin();
for(;va_the!=the->second.end();va_the++)
{
if(va_the->name==name)
{
string name;
string phone;
string addr;
cout<<"please input newInfo"<<endl;
cout<<"name phone addr"<<endl;
cin>>name>>phone>>addr;
va_the->name=name;
va_the->phone=phone;
va_the->addr=addr;
return true;
}
}
}
bool delet(map<char,vector<msg> >& phone,const char* name)
{
if(!find(phone,name))
return false;
char* p=(char*)name;
map<char,vector<msg> >::iterator the;
the=phone.find(change(*p));//找到首字母所在目录//一定要注意大小写转换!!
vector<msg>::iterator va_the;
va_the=the->second.begin();
for(;va_the!=the->second.end();va_the++)
{
if(va_the->name==name)
{
the->second.erase(va_the);
return true;
}
}
}
int main()
{
map<char,vector<struct msg> > phone;
//>>之间必须空格,不然会与输出流重复而歧义
struct msg e[3]={{"cmz","15916343960","wuhan"},
{"qj","1234567890","shiyan"},
{"&wzl","0987654321","hangkou"}};
init(phone);
insert(phone,e[0]);
insert(phone,e[1]);
insert(phone,e[2]);
// find(phone,"ll");
// mod(phone,"cmz");
delet(phone,"cmz");
show(phone);
}
简单通讯录
最新推荐文章于 2022-04-10 20:15:06 发布