CH8 课后习题
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 课后习题的更多相关文章
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- web实验指导书和课后习题参考答案
实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...
- 《Python核心编程》 第六章 序列 - 课后习题
课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...
- 《Python核心编程》 第五章 数字 - 课后习题
课后习题 5-1 整形. 讲讲 Python 普通整型和长整型的区别. 答:普通整型是绝大多数现代系统都能识别的. Python的长整型类型能表达的数值仅仅与你机器支持的(虚拟)内存大小有关. 5- ...
- 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总
大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...
- OpenCV学习笔记之课后习题练习3-5
OpenCV学习笔记之课后习题练习2-5 练习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图像中使用ROI和cvSet()建立一个增长如金字塔状的数组. 参考博文:www ...
- OpenCV学习笔记之课后习题练习2-5
5.对练习4中的代码进行修改,参考例2-3,给程序加入滚动条,使得用户可以动态调节缩放比例,缩放比例的取值为2-8之间.可以跳过写入磁盘操作,但是必须将变换结果显示在窗口中. 参考博文:blog.cs ...
- OpenCV学习笔记之课后习题练习2-3
3.使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘. 例2-10中所用的方法虽然能正常运行,但却不能 ...
- OpenCV学习笔记之课后习题练习3-4
练习:创建一个大小为100*100的三通道RGB图像.将它的元素全部置0.使用指针算法以(20,5)与(40,20)为顶点绘制一个绿色平面. 参考博文:blog.csdn.net/qq_2077736 ...
随机推荐
- 25 JavaScript对象原型&ES5新的对象方法
JavaScript对象原型 所有JavaScript对象都从原型继承对象和方法 日期对象继承自Date.prototype,数组继承自Array.prototype,对象构造器新建的对象Person ...
- C++11常用特性介绍——for循环新用法
一.for循环新用法——基于范围的for循环 for(元素类型 元素对象 : 容器对象) { //遍历 } 1)遍历字符串 std::string str = "hello world&qu ...
- linux文件或目录属性
wc(word count)命令的功能:统计指定文件的字节数.字数.行数.,并将统计结果显示输出 命令参数: -c 只显示字节数 -l 只显示行数 -w 只显示字数 od命令:查看二进制文件信息 ...
- knockout 简单使用
定义: var QcViewModel = function () { var self = this; self.name = ko.observable(); self.qty = ko.obse ...
- 8.1.1默认的map函数、reduce函数、分区函数
1.1.1 默认的map函数和reduce函数 (1)Maper和Reuducer默认类 如果没有指定maper类和reduce类,则会用默认的Maper和Reuducer类去处理数据 ...
- ubuntu开启mysql远程连接,并开启3306端口
mysql -u root -p 修改mysql库的user表,将host项,从localhost改为%.%这里表示的是允许任意host访问,如果只允许某一个ip访问,则可改为相应的ip mysql& ...
- BZOJ 4167: 永远的竹笋采摘
首先同BZOJ5052 \(O(n \log n \log v)\) 求出所有点对 现在变成选出 \(k\) 条不相交的线段使得权值最小 可用前缀min优化dp \(O(nk)\) 解决 还是太慢,考 ...
- 无线渗透之ettercap
无线渗透之ettercap ettercap命令查看 # ettercap -h Usage: ettercap [OPTIONS] [TARGET1] [TARGET2] TARGET is in ...
- 实用类-<字符串与基本类型的转换>
字符串与基本类型的转换 字符串->基本类型 int i5=Integer.parseInt("123"); System.out.println(i5); 基本类型-> ...
- overlay rate
1.导入nii.img文件,三维矩阵 2.模版矩阵和网络矩阵对应位置元素相乘 .* 3.生成位置为0的新矩阵 cc=(nii_new==0); 4.两个矩阵的非零元素个数 t1=length(ni ...