简单通讯录

#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);



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值