最近都忙着看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 ope