STL中的vector应用---简单通讯录

在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;
}
运行结果:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值