8.1&&8.2

 #include <iostream>
#include <vector>
#include <string> using namespace std; istream &func(istream &is)
{
int val;
while (is >> val && !is.eof()) {
cout << val << endl;
}
is.clear(); //复位所有错误标志位
} int main()
{
func(cin);
return ;
}

8.3

读取类型不匹配;遇到文件结束符EOF;IO流错误

8.4

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; int main()
{
vector<string> vec;
string ifile = "data.txt", ss; //需在本文件夹下有data.txt这个文件
ifstream in(ifile);
while(getline(in, ss)) {
vec.push_back(ss);
}
in.close();
for (auto i : vec)
cout << i << endl;
return ;
}

8.5

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; int main()
{
vector<string> vec;
string ifile = "data.txt", ss; //需在本文件夹下有data.txt这个文件
ifstream in(ifile);
while(in >> ss) { //改动处
vec.push_back(ss);
}
in.close();
for (auto i : vec)
cout << i << endl;
return ;
}

8.6

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; struct Sales_data {
string bookNo; //书的ISBN
unsigned units_sold = ; //售出的本数
double revenue = 0.0; //销售额
Sales_data& combine(const Sales_data &);
string isbn() { return bookNo; }
}; Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
} ostream &print(ostream &os, const Sales_data &item)
{
os << item.bookNo << " " << item.units_sold << " " << item.revenue;
return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
} int main(int argc, char *argv[])
{
if (argc != ) {
cerr<< "Please give the file name."<<endl;
return -;
}
ifstream in(argv[]);
if (!in) {
cerr<<"Can't open the file."<<endl;
return -;
}
Sales_data total;
if (read(in, total)) {
Sales_data trans;
while (read(in, trans)) {
if (total.isbn() == trans.isbn()) total = add(total, trans);
else {
print(cout, total);
cout << endl;
total = trans; //结构体赋值很方便
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
}
else {
cerr << "No data?!" << endl;
return -;
}
return ;
}

文件名(book.txt)通过参数传给main函数:

8.7

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; struct Sales_data {
string bookNo; //书的ISBN
unsigned units_sold = ; //售出的本数
double revenue = 0.0; //销售额
Sales_data& combine(const Sales_data &);
string isbn() { return bookNo; }
}; Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
} ostream &print(ostream &os, const Sales_data &item)
{
os << item.bookNo << " " << item.units_sold << " " << item.revenue;
return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
} int main(int argc, char *argv[])
{
if (argc != ) {
cerr<< "Please give the input file name and output file name."<<endl;
return -;
}
ifstream in(argv[]);
ofstream out(argv[]);
if (!in) {
cerr<<"Can't open the file."<<endl;
return -;
}
if (!out) {
cerr<<"can't open output file."<<endl;
return -;
}
Sales_data total;
if (read(in, total)) {
Sales_data trans;
while (read(in, trans)) {
if (total.isbn() == trans.isbn()) total = add(total, trans);
else {
print(out, total) << endl; //输出到指定文件中
total = trans;
}
}
print(out, total) << endl;
}
else {
cerr << "No data?!" << endl;
return -;
}
return ;
}

将上一题输出的内容输出到文件book1.txt中:

8.8

 #include <iostream>
#include <fstream>
#include <vector>
#include <string> using namespace std; struct Sales_data {
string bookNo; //书的ISBN
unsigned units_sold = ; //售出的本数
double revenue = 0.0; //销售额
Sales_data& combine(const Sales_data &);
string isbn() { return bookNo; }
}; Sales_data &Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} istream &read(istream &is, Sales_data &item)
{
is >> item.bookNo >> item.units_sold >> item.revenue;
return is;
} ostream &print(ostream &os, const Sales_data &item)
{
os << item.bookNo << " " << item.units_sold << " " << item.revenue;
return os;
} Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
} int main(int argc, char *argv[])
{
if (argc != ) {
cerr<< "Please give the input file name and output file name."<<endl;
return -;
}
ifstream in(argv[]);
ofstream out(argv[], ofstream::app); //唯一改变之处
if (!in) {
cerr<<"Can't open the file."<<endl;
return -;
}
if (!out) {
cerr<<"can't open output file."<<endl;
return -;
}
Sales_data total;
if (read(in, total)) {
Sales_data trans;
while (read(in, trans)) {
if (total.isbn() == trans.isbn()) total = add(total, trans);
else {
print(out, total) << endl;
total = trans;
}
}
print(out, total) << endl;
}
else {
cerr << "No data?!" << endl;
return -;
}
return ;
}

8.9

 #include <iostream>
#include <vector>
#include <string>
#include <sstream> using namespace std; int main()
{
string line, word;
while(getline(cin, line)) {
istringstream in(line);
while (in >> word)
cout << word << endl;
}
return ;
}

8.10

 #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream> using namespace std; int main()
{
vector<string> vec;
string line, word;
ifstream input("data.txt");
while(getline(input, line)) {
vec.push_back(line);
}
for (auto i : vec) {
istringstream in(i);
while (in >> word) {
cout << word << endl;
}
}
return ;
}

8.11

 #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream> using namespace std; struct PersonInfo {
string name;
vector<string> phones;
}; int main()
{
string line, word;
vector<PersonInfo> people;
istringstream record; //定义在循环之外
while (getline(cin,line)) {
PersonInfo info;
record.clear(); //每次要解除上次绑定的string对象
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
return ;
}

8.12

因为此时我们需要其中的成员皆可被访问,所以 PersonInfo 是一个聚合类,不能在内部进行初始化

8.13

 #include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream> using namespace std; struct PersonInfo {
string name;
vector<string> phones;
}; int main()
{
ifstream in("data.txt"); //从文件读取
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(in,line)) { //in为文件输入流
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
return ;
}

8.14

这两条语句分别适用范围for语句枚举people中所有项和每项的phones中的所有项。

使用const表明在循环中不会改变这些项的值;

使用auto是请求编译器依据vector元素类型来推断出entry和nums的类型,既简化代码又避免出错;

使用引用的原因是,people和phones的元素分别是结构对象和字符串对象,使用引用即可避免对象拷贝。

最新文章

  1. Java
  2. Fiddler (三) Composer创建和发送HTTP Request
  3. 解决eclipse中android添加重载函数时参数为arg0,arg1的问题
  4. IOS之UI--小实例项目--添加商品和商品名(纯代码终结版)
  5. HDU-4687 Boke and Tsukkomi 带花树,枚举
  6. 转:windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)
  7. IllegalArgumentException: Does not contain a valid host:port authority: master:8031
  8. CentOS 5设置服务器hostname、DNS和IP
  9. Unity 3d 刚体
  10. 获取第上一个兄弟元素 屏蔽浏览器的差异(PreviousElementSibling)
  11. Velocity.js的使用
  12. 用java从0生成一个简单的excel
  13. linux命令-diff对比文件工具
  14. Echarts的一些总结
  15. Linux(CentOS 7)命令行模式安装VMware Tools 详解
  16. linux几个命令
  17. HTML(六)--总结
  18. 记录一下寄几个儿的greendao数据库升级,可以说是非常菜鸡了嗯
  19. The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
  20. python 复习函数 装饰器

热门文章

  1. python装饰器内获取函数有用信息方法
  2. jQuery语法、选择器、效果等使用
  3. webpack-dev-server 多入口自动刷新,支持对象
  4. ;(function($,window,document,undefined){})(jQuery,window,document)
  5. 记一次PHP实现接收邮件信息(我这里测试的腾讯企业邮件)
  6. Delphi7 GDI+学习
  7. python生成器函数中return的作用
  8. 『Python基础-8』列表
  9. 『Python基础-6』if语句, if-else语句
  10. 树莓派3B的WiFi中文乱码及搜索不到附近的WiFi_解决方案: