//创建电话本
//假定有一个文件,列出了一些人和他们的电话号码,某些人只有一个号码,而另一些人则有多个-home,work,mobile etc.创建一个程序可以从标准输入读取
//每个人的名字和电话号码,每个人的信息占据一行,结束输入后,将这些人的信息存储到文件中,并打印出来
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<fstream>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phonenumber;
};
//重载<<运算符
ostream& operator<<(ostream& oos, PersonInfo &PI)
{
oos << PI.name << " ";
for (vector<string>::iterator it = PI.phonenumber.begin(); it != PI.phonenumber.end(); ++it)
oos << *it;
return oos;
}
int main(int argc, char *argv[])
{
string line, word;
PersonInfo PI;
vector<PersonInfo> InfoList;
ofstream fs;
fs.open("phonebook.txt", ofstream::out);
while (cin >> line)
{
fs << line<<endl;
istringstream info(line);
info >> PI.name;
while (info >> word)
PI.phonenumber.push_back(word);
InfoList.push_back(PI);
}
fs.close();
for (vector<PersonInfo>::iterator it = InfoList.begin(); it != InfoList.end(); ++it)
{
cout << *it;
}
system("pause");
return 0;
}
创建电话本
最新推荐文章于 2020-11-29 14:19:10 发布