ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

C++ //练习 8.13 重写本节的电话号码程序,从一个命名文件而非cin读取数据。

2026/9/19 10:15:46 拓冰建站 浏览量
C++ //练习 8.13 重写本节的电话号码程序,从一个命名文件而非cin读取数据。

C++ Primer(第5版) 练习 8.13

练习 8.13 重写本节的电话号码程序,从一个命名文件而非cin读取数据。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************> File Name: ex8.13.cpp> Author: > Mail: > Created Time: Sun 25 Feb 2024 07:49:55 PM CST************************************************************************/#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;struct PersonInfo{string name;vector<string> phones;
};int main(int argc, char *argv[]){ifstream input(argv[1]);ofstream output(argv[2], ofstream::app);string line, word;vector<PersonInfo> people;while(getline(input, line)){PersonInfo info;istringstream record(line);record>>info.name;while(record>>word){info.phones.push_back(word);}people.push_back(info);}for(const auto &p : people){ostringstream out;for(const auto &h : p.phones){out<<" "<<h;}output<<p.name<<" "<<out.str()<<endl;}return 0;
}
运行结果显示如下

在这里插入图片描述