Bjarne Stroustrup第10.5章示例
Bjarne Stroustrup chapter 10.5 example
这是关于Bjarne Stroustrup的《使用C++的原理和实践》一书第10.5节中的一个例子。据我所知,它应该提示用户输入要创建的文件的名称(所以我键入了probe.txt
),然后它应该要求用户打开一个文件(所以我再次键入probe.txt
),然后程序跳过我的while
语句并返回0。我应该如何输入时间和温度?
#include <std_lib_facilities.h>
struct Reading {
int hour;
double temperature;
};
int main()
{
cout << "Please enter input file name: n";
string iname;
cin >> iname;
ifstream ist{iname};
string oname;
cout << "Please enter output file name: n";
cin >> oname;
ofstream ost{oname};
vector<Reading> temps;
int hour;
double temperature;
while (ist >> hour >> temperature) {
temps.push_back(Reading{hour,temperature});
}
keep_window_open();
return 0;
}
当您看到提示时:
cout << "Please enter input file name: n";
它询问您要从哪个文件中读取数据。
当您看到提示时:
cout << "Please enter output file name: n";
它会问你想写什么文件。
请注意关键字input和output的区别。
此循环:
while (ist >> hour >> temperature) {
temps.push_back(Reading{hour,temperature});
意思是说,当ist(输入文件流)返回一个好值(意味着它还没有到达文件的末尾)时,我们向Vector添加一个类型为Reading的项,称为"temps"。(Vector本质上是一个列表容器类型)我们从文件中的行中抓取的两个项目中创建了一个Reading类型的项目。
概括一下,我们从文件中的文本中读取,然后将其添加到名为"temps"的向量中
">>"是读取文件中下一项的运算符。在代码中,它读取接下来的两个项目,并将它们输入小时和温度。
相关文章:
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 你好世界在 APUE 第 7 章退出,代码为 0
- C++:引用引用,Stroustrup示例
- 关于Stroustrup C++书的自动和{}列表问题
- 如何从Stroustrup的C++书中解释这个正则表达式?
- C++:使用Stroustrup示例生成随机数?
- Stroustrup的C++书挑战赛,有人可以帮助我理解这段代码吗?
- 如何使用Stroustrup的Fltk Text类调用变量?
- Bjarne Stroustrup PPP - 第 9 章 演练 4 - cout 枚举错误
- Stroustrup Ex.7, Chap.4 - C++ Syntax
- 使用"cin"进行演练 #7 获得不同的结果 编程:使用C++的原则和实践 (Stroustrup) 第 4 章
- 来自 Stroustrup PPP 的伪随机数生成器,使用 C++,第 8 章,练习 8 和 9
- stroustrup ppp第8章钻头头部
- 正在学习《编程原理与使用C++的实践》第4章中的演练.Bjarne Stroustrup
- 'Programming: Principles and Practice Using C++'(第2版),作者:Bjarne stroustrup,第3.3章:代码不起作用
- Stroustrup PPP Book第12章:无法转换'& # 39; Graph_lib::点# 39;
- 视觉工作室 - 编译C++程序时VS 2013中的链接时间错误 - B. Stroustrup的PPP使用C++:第8章 - Q1 钻探?
- Bjarne Stroustrup第10.5章示例
- 视觉 作者的代码是否正确?B. Stroustrup的PPP使用C++:第7章,第7.2节 - 输入和输出