用STL写的 通讯录

博主分享了自己在阅读《C++ Primer》后,运用学到的IO、关联容器和顺序容器知识编写的一个通讯录程序。程序中涉及了vector、list、map等容器以及find泛型算法,通过迭代器进行查找操作,并实现了文件的读写功能,特别是文件流和缓冲流的应用。尽管博主承认对IO操作掌握不够熟练,导致代码不够精炼,但计划以面向对象的方式重新设计以优化程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      最近都忙着看c++primer  这本书 。刚刚看完IO 关联容器和顺序容器。看完之后,我用所学的知识写了一个通讯录的程序。

     在这个程序里 主要是用了vector list 还有map 等容器,还用了少量的泛型算法,比如find这个函数用来查找姓名的算法。主要用了迭代器来进行查找。进行了文件的新建和储存。尤其是文件流,缓冲流 等。虽然现在对IO还不是很会运用 ,但是已经基本了解其运用。

    尤须知识点的不熟悉,所以代码不是很精练,下次,我将用面向对象的形式重写这个程序。使其更加精练!

   下面附上这个程序的代码:

     

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<iomanip>
#include<algorithm>
using namespace std;
ifstream &open_file(string &file,ifstream &in)//打开文件 并显示所有联系人
{
	in.clear();
	in.close();
	in.open(file.c_str());
	if(!in)
	{
		cerr<<"sorry,the file cann't file!"<<endl;
		return in;
	}
	vector<string> s;
	string s1;
	int i=0;
	while(in>>s1,!in.eof())
	{
		s.push_back(s1);
		if(in.bad())
			throw runtime_error("IO stream corrupted");
		if(in.fail())
		{
			cerr<<"bad date,try again"<<endl;
			in.clear();
			in.ignore();
			continue;
		}
		cout<<s1<<" ";
		i++;
		if(i %2 == 0)
			cout<<endl;
	}
	return in;
}
fstream &addlinkman(string &file,fstream &in)//添加联系人
{
	in.open(file.c_str());//打开文件
	if(!in)
	{
		cerr<<"sorry,you cann't open the file"<<endl;
		return in;
	}
	map<string,string> linkman;
	string key,value;
	while(in>>key>>value,!in.eof())
	{
		linkman.insert(make_pair(key,value));
	}
	in.close();
	in.clear();
	string name,number;
	cout<<"请输入"<<endl;
	cout<<"联系人姓名   联系人电话"<<endl;
	cin>>name>>number;
	map<string,string>::iterator it = linkman.find(name);
	if(it!=linkman.end())
	{
		cout<<"联系人已经存在,你输入的号码将覆盖旧号码,是否执行此操作。Y or N"<<endl;
		char decide;
		cin>>decide;
		if(decide == 'N'||decide =='n')
			return in;
		else
			it->second = number;
		/* the  other method:
		    linkman[name] = number;
		*/
	}
	linkman.insert(make_pair(name,number));
     	/* the other method:
     	linkman[name]=number;因为在map中如果没有name这个key,则会自动添加 ,并自动付给他一个值。
     	*/
	in.open(file.c_str(),ofstream::out);
	for(map<string,string>::iterator it = linkman.begin();it!=linkman.end();it++)
	{
		in<<it->first<<setw(20)<<it->second<<endl;
	}
	in.close();
	in.clear();
	return in;
}
ifstream &rmadlinkman(string &file)//删除联系人
{
	string cinadd;
	vector<string> add;
	ifstream open_file(file.c_str());
	if(!open_file)
	{
		cerr<<"sorry,you can't open the file"<<endl;
		return open_file;
	}
	string s;
	while(open_file>>s)
	{
		add.push_back(s);
	}
	open_file.close();
	cout<<"请输入你想删除的联系人:";
	ofstream open_file1(file.c_str(),ofstream::out);
	if(!open_file1)
		cerr<<"the file cannot open"<<endl;
	cin>>cinadd;
	for(vector<string>::iterator it =add.begin();it!=add.end(); it++)
	{
		if( *it == cinadd )
		{
			it = add.erase(it);
			it =add.erase(it);
			cout<<"删除成功"<<endl;
			break;
		
		}
	}
	for(vector<string>::iterator it=add.begin();it!= add.end();it++)
		open_file1<<*it<<endl;
	open_file1.close();
	return open_file;
}
ifstream &showadlinkman(string &file,ifstream &in)//查找联系人
{
	in.clear();
	in.close();
	in.open(file.c_str());
	if(!in)
	{
		cerr<<"sorry,the file can't open"<<endl;
		return in;
	}
	map <string,string> msi;
	string s, s1;
	while(in>>s>>s1,!in.eof())
	{
		msi.insert(make_pair(s,s1));
	}
	in.close();
	string findword;
	cin>>findword;
	/*for(vector<string>::iterator it =svec.begin();it!=svec.end();it++)
	{
		if(*it == findword)
		{
			cout<<*it<<setw(20)<<*(it+1)<<endl;
			break;
		}
		if(it == svec.end())
		{   
		    cout<<"对不起,你搜索的联系人不存在"<<endl;
		}
	}*/
	//另一种方法。
	map<string,string>::iterator it =msi.find(findword);//find的泛型算法
	if(it == msi.end())
	{
		cout<<"对不起,你寻找的联系人不存在"<<endl;
	}
	else
		cout<<it->first<<setw(20)<<it->second<<endl;
	return in;
}
fstream &exchangeadlinkman(string &file,fstream &in)//更改联系人
{
	in.clear();
	in.close();
	in.open(file.c_str());
	if(!in)
	{
		cerr<<"sorry ,you cann't open the file "<<endl;
		return in;
	}
	string s;
	vector<string> svec;
	while(in>>s,!in.eof())
	{
		svec.push_back(s);
	}
	in.close();
	in.clear();
	string word;
	cin>>word;
	vector<string>::iterator it = find(svec.begin(),svec.end(),word);
	if(it == svec.end())
	{
		cout<<"对不起,你想更改的联系人不存在"<<endl;
		return in;
	}
	else
	{
		cout<<"请输入你更改号码:";
		cin>>*(it+1);
		cout<<"更改成功"<<endl;
	}
	in.open(file.c_str(),fstream::out);
	for(vector<string>::iterator it=svec.begin();it != svec.end();it ++)
	{
		in<<*it<<endl;
	}
	in.close();
	return in;
}
int main()
{  
	ifstream addresslist;
	ofstream addresslist2;
	fstream addresslist1;
	string file_add;
	system("color 42");
	cout<<"欢迎你使用本通讯录"<<endl;
	cout<<"请输入你想使用的通讯录,如果通讯录不存在则会自动创建"<<endl;
	cout<<"默认的通讯录为addresslist.atp"<<endl;
	cout<<"请输入你想打开(创建)的文件名:";
	ofstream in;
	cin>>file_add;
	in.open(file_add.c_str());
	if(!in)
	{
		cout<<"文件不存在,即将创建"<<endl;
		in.open(file_add.c_str(),ios::out);
	}
	system("cls");
	while(1)
	{
		cout<<"1.添加通讯录   2.删除通讯录  3.显示通讯录  4.查找联系人  5.更改联系人  6.退出"<<endl;
		int number;
    	cin>>number;
    	switch(number)
		{
     		case 1:
     			cout<<"添加通讯录"<<endl;
 				addlinkman(file_add,addresslist1);
				system("cls");
				break;
    		case 2:
	    		cout<<"删除通讯录"<<endl;
				rmadlinkman(file_add);
				system("cls");
				break;
	     	case 3:
	     		cout<<"显示通讯录"<<endl;
				open_file(file_add,addresslist);
				break;
	    	case 4:
				cout<<"查找联系人"<<endl;
				showadlinkman(file_add,addresslist);
				break;
	    	case 5:
		    	cout<<"更改通讯录"<<endl;
				exchangeadlinkman(file_add,addresslist1);
				system("cls");
				break;
	    	case 6:
	     		cout<<"谢谢使用 请按任意键退出"<<endl;
		    	return 0;
	     	default:
	     		cout<<"对不起 你输入有误"<<endl;
				break;
		}
		cout<<endl;
	}
	system("pause");
	return 0;
}
     写得不好 ,还请各位多多指教!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值