8.1和8.2

 #include <iostream>

 using namespace std;

 istream& f(istream& in)
{
int v;
in >> v;
if (in.bad())
throw runtime_error("IO is bad");
if (in.fail())
{
cerr << "bad data,try again" << endl;
in.clear();
in.ignore(, '\n');
//忽略的知识点continue只能用在循环中
}
cout << v << endl;
in.clear();
return in;
}
int main()
{
cout << "please enter numbers:(Ctrl+Z to end)" << endl;
f(cin);//cin遇到空格会结束,如果要读取整行,可以使用getline
system("pause");
return ;
}

8.3

遇到了文件结束符标志,或者是输入错误的流或无效数据

8.4

 #include <iostream>
#include <vector>
#include <string>
#include <fstream> using namespace std; int main()
{
string fileName;
cout << "please enter the name of file:" << endl;
cin >> fileName;
ifstream infile(fileName);
if (!infile)
{
cerr << "can not open the file you entered,please check!" << endl;
return -;
}
string line;
vector<string> row;
while (getline(infile, line))
{
row.push_back(line);
}
for (auto it : row)
cout << it << endl; system("pause");
return ;
}

8.5

将8.4的第21行改为while(infile>>line)即可

8.9

 #include <iostream>
//#include <vector>
#include <sstream>
#include <string> using namespace std; istream& f(istream& in)
{
string str; //in >> str;//遇到空格就结束,使用循环
while (in >> str, !in.eof())
{
if (in.bad())
throw runtime_error("IO IS bad!");
if (in.fail())
{
cerr << "bad data or not matched" << endl;
in.clear();
in.ignore(, '\n');
continue;
}
cout << str << endl;
} } int main()
{
ostringstream os;
os << "Hello C++" << endl;;
istringstream in(os.str());
f(in);
system("pause");
return ;
}

8.10

 #include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream> using namespace std; int main()
{
string fileName;
cout << "please enter the name of the file:" << endl;
cin >> fileName;
ifstream infile(fileName);
if (!infile)
{
cerr << "can not open the file" << endl;
return -;
} string line;
vector<string>txt_row;
while (getline(infile, line))
{
txt_row.push_back(line);
//istringstream record(line);//题目要求从vector中读取数据,不是从文件,所以这样不符合要求
}
infile.close();
for (auto it : txt_row)
{
istringstream line_str(it);
string word;
while (line_str >> word)
cout << word << " ";
cout << endl; }
system("pause");
return ;
}

8.11

istringstream 的对象record若定义在while循环外,则会被循环调用,重复使用流,需要恢复流状态,使用clear恢复

 #include <iostream>
#include <vector>
#include <sstream> using namespace std;
struct PersonInfo {
string name;
vector<string>phone; }; int main()
{ string line;
string word;
vector<PersonInfo>person;
istringstream record;
while (getline(cin, line))
{
PersonInfo info;
record.clear();
record >> info.name;
while (record >> word)
{
info.phone.push_back(word); }
person.push_back(info);
}
system("pause");
return ;
}

8.12

因为每个人的电话号码数量不固定。

8.13

此题,我写的存在问题,测试时和我的文件分别是这样的

 #include <iostream>
#include <fstream>
#include <sstream>
#include <vector> using namespace std; struct PersonInfo {
string name;
vector<string>phone;
}; bool valid(const string& str)
{
cout << "check the string valid or not" << endl;
return true;
} //string format(string& str)
string format(const string& str)
{
cout << "format the phone numbers " << endl;
return str;
}
int main(int argc,char*argv[])
{
string line;
string word;
vector<PersonInfo> person;
istringstream record;
if (argc != )
{
cerr << "please enter the name of file." << endl;
return -;
}
ifstream infile(argv[]);
if (!infile)
{
cerr << "can not open the file" << endl;
return -;
}
while (getline(infile, line))
{
PersonInfo info;
infile.clear();
record.str(line);
record >> info.name; while (record >> word)
{
info.phone.push_back(word);
}
person.push_back(info);
} //实现将从文件读取的记录输出
ostringstream os;
for (const auto& entry : person)
{
ostringstream badNums;
ostringstream formatted;
for (auto &nums : entry.phone)
{
if (!valid(nums))
{
//badNums << nums << " is invalid" << endl;
badNums << " " << nums; }
else
//formatted << "after foramtted: " << format(nums) << endl;
formatted << " " << format(nums);
} if (badNums.str().empty())
os << entry.name << " " << formatted.str() << endl;
else
cerr << "input error,try again" << endl; }
cout << os.str() << endl;//
//system("pasue");
return ;
}

另外,我认为format函数的参数应该是非const 型的,但是若是非const型,则62行就不能是引用型,否则编译出错,我暂时还没想明白原因

8.14

如上,使用引用是因为person和phone的元素分别是struct和string对象,使用引用可避免拷贝,但是教材中定义为const,我就无法理解了,通常定义为const是避免改变这些项的值,name成员不能改变,可是phone成员需要格式化处理,需要改变

CH8 课后习题的更多相关文章

  1. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  2. web实验指导书和课后习题参考答案

    实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...

  3. 《Python核心编程》 第六章 序列 - 课后习题

    课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...

  4. 《Python核心编程》 第五章 数字 - 课后习题

    课后习题  5-1 整形. 讲讲 Python 普通整型和长整型的区别. 答:普通整型是绝大多数现代系统都能识别的. Python的长整型类型能表达的数值仅仅与你机器支持的(虚拟)内存大小有关. 5- ...

  5. 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总

    大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...

  6. OpenCV学习笔记之课后习题练习3-5

    OpenCV学习笔记之课后习题练习2-5 练习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图像中使用ROI和cvSet()建立一个增长如金字塔状的数组. 参考博文:www ...

  7. OpenCV学习笔记之课后习题练习2-5

    5.对练习4中的代码进行修改,参考例2-3,给程序加入滚动条,使得用户可以动态调节缩放比例,缩放比例的取值为2-8之间.可以跳过写入磁盘操作,但是必须将变换结果显示在窗口中. 参考博文:blog.cs ...

  8. OpenCV学习笔记之课后习题练习2-3

    3.使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘. 例2-10中所用的方法虽然能正常运行,但却不能 ...

  9. OpenCV学习笔记之课后习题练习3-4

    练习:创建一个大小为100*100的三通道RGB图像.将它的元素全部置0.使用指针算法以(20,5)与(40,20)为顶点绘制一个绿色平面. 参考博文:blog.csdn.net/qq_2077736 ...

随机推荐

  1. Docker Learning Notes

    Docker简介 是什么 问题:为什么会有docker出现 一款产品从开发到上线,从操作系统,到运行环境,再到应用配置.作为开发+运维之间的协作我们需要关心很多东西,这也是很多互联网公司都不得不面对的 ...

  2. DMVPN基础配置

    DMVPN基础拓扑: 配置步骤:    1. 基本IP地址配置实现网络可达    2. 配置GRE多点隧道(mGRE)和NHRP(下一跳解析协议)    3. 配置EIGRP路由协议    4. 配置 ...

  3. Nexus-vPC理论

    vPC:virtual Port-channel 1.vPC的作用: • 允许一个设备使用2个上游的设备的端口来实现Port Channel    • 消除STP阻止端口的情况    • 提供一个无环 ...

  4. 设计模式六大原则——开放封闭原则(OCP)

    什么是开闭原则? 定义:是说软件实体(类.模块.函数等等)应该可以扩展,但是不可修改. 开闭原则主要体现在两个方面: 1.对扩展开放,意味着有新的需求或变化时,可以对现有代码进行扩展,以适应新的情况. ...

  5. 理解js中的原型链

    对象有”prototype”属性,函数对象有”prototype”属性,原型对象有”constructor”属性. 关于原型 在JavaScript中,原型也是一个对象,通过原型可以实现对象的属性继承 ...

  6. 到头来还是逃不开Java - Java13面向对象基础

    面向对象基础 没有特殊说明,我的所有学习笔记都是从廖老师那里摘抄过来的,侵删 引言 兜兜转转到了大四,学过了C,C++,C#,Java,Python,学一门丢一门,到了最后还是要把Java捡起来.所以 ...

  7. GCC 升级

    1.下载源码 wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.8.5/gcc-4.8.5.tar.gz 2.下载依赖包编译安装 GCC 需要依赖 mpc,mpfr,gmp包. ...

  8. Java基础知识笔记第七章:内部类和异常类

    内部类 /* *Java支持在一个类中定义另一个类,这样的类称为内部类,而包含内部类的类称为内部类的外嵌类 */ 重要关系: /* *1.内部类的外嵌类在内部类中仍然有效,内部类的方法也可以外嵌类的方 ...

  9. centos将uwsgi添加为系统服务

    如果退出ssh 链接, 都会导致uwsgi进程关闭 这时, 我们需要进行管理软件管理uwsgi进行的运行, centos系统中我们采用 systemd, 让我们的项目变为系统服务 第一步: 首先 vi ...

  10. HDU1029 简单DP

    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. &quo ...