使用istream参数读取的istream函数 [英] Istream function to read with istream parameter
问题描述
我正在尝试理解此代码
istream &read(istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
ostream &print(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
}
我不明白这些功能是什么,我也不明白为什么我们使用 istream
进行阅读而使用 ostream
进行打印,而不是使用 cin
和 cout
。
I don't understand what are these function , also I can't understand why we use istream
for reading and ostream
for printing instead of using cin
and cout
.
推荐答案
还有很多您需要了解一些细微的作用,以了解 read
和 print
函数中正在发生的事情。从概述的角度来看, istream
和 ostream
是通用输入和输出类,它们为C ++中的流I / O提供了基础。正如其他答案和注释正确指出的那样, std :: cin
和 std :: cout
源自 istream
和 ostream
提供标准流 stdin
和 stdout
。
There are more than a few subtleties at play that you need to understand to understand what is taking place in the read
and print
functions. From the overview standpoint istream
and ostream
are general input and output classes that provide a basis for stream I/O in C++. As the other answer and comments correctly indicate, std::cin
and std::cout
are derived from istream
and ostream
to provide input/output from the standard stream stdin
and stdout
.
流本身具有确定流是否良好的状态,可以从中读取或写入或是否发生了流错误,从而阻止了进一步的I / O(某些故障是可恢复的,有些无法恢复),请参见 std :: basic_ios :: rdstate 和 std :: basic_ios :: setstate 讨论构成流状态的位( goodbit,badbit,failbit,eofbit
)。
The stream themselves have a state that determines if the stream is good and can be read from or written to, or whether a stream error has occurred preventing further I/O (some failures are recoverable, some not) See std::basic_ios::rdstate and std::basic_ios::setstate for a discussion of the bits (goodbit, badbit, failbit, eofbit
) that make up the stream state.
现在查看您的函数原型:
Now looking at your function prototyes:
istream &read(istream &is, Sales_data &item)
和
ostream &print(ostream &os, const Sales_data &item)
请注意第一个参数如何引用当前流?并注意返回也是如何引用相同的流的?这很重要,因为它传递了对流的引用,因此在函数中对 Steam状态进行的任何更改都可以在返回时使用。因此,如果在 read
函数中遇到 eof
,则状态更改将在返回时可用。与 print
函数相同(尽管会改变流的潜在错误更少且有所不同)
Notice how the first parameter is a refernce to the current stream? And notice how the return is also a reference to that same stream? That's important, because it passes a reference to the stream so any changes to the Steam State occurring within the function will be available on return. So if eof
is encountered within the read
function, that change in state will be available on return. The same for your print
function (though the potential errors that would change the stream are fewer and different)
read
,您正在读取流中的3条信息, item.bookNo,item.units_sold
和价格
并更新 item.revenue
:
Within read
, you are reading 3-pieces of information from the stream, item.bookNo, item.units_sold
and price
and updating item.revenue
:
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
您可以从 std :: cin
中读取通过将其作为的参数是
,或者可以传递打开的文件流。流操作将对任何有效流都有效。
You can read from std::cin
by passing that as the argument for is
or you can pass an open file-stream. The stream operations will work for any valid stream.
print函数执行相反的操作,它输出 item.isbn()返回.units_sold,item.revenue
和 item.avg_price()
的返回作为输出。如果您通过 std :: cout
作为 os
The print function does the opposite, it outputs the return of item.isbn(), item.units_sold, item.revenue
and the return from item.avg_price()
as output. If you pass std::cout
as os
这两个函数中的最后一个命令返回流,包括流状态中的任何更改,因此调用方可以检查读取
还是打印
成功。这是呼叫者能够确定是否发生I / O的关键。
The last command in both functions returns the stream including any change in the stream state so the caller can check whether the read
or print
was successful. That is key for the caller being able to determine whether the I/O took place.
过度简化的示例
使用该函数的过于简化的示例可能有助于使概念陷入其中。在这里,我们声明一个简化的结构 Sales_data
和 int bookno,units_sold;
和双重价格,收入;
例如:
An overly simplified example exercising the function may help the concepts sink in. Here we declare a simplified struct Sales_data
with int bookno, units_sold;
and double price, revenue;
For example:
#include <iostream>
struct Sales_data
{
int bookno, units_sold;
double price, revenue;
};
我们然后将 read
简化为仅读取 bookno,units_sold
和 price
并为此计算收入
单个项目,例如
We then simplify read
to read only the bookno, units_sold
and price
and compute the revenue
for that single item, e.g.
std::istream &read(std::istream &is, Sales_data &item)
{
is >> item.bookno >> item.units_sold >> item.price;
item.revenue = item.price * item.units_sold;
return is;
}
并简化 print
只输出 units_sold
和收入
:
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << "units sold: " << item.units_sold << " revenue: $" << item.revenue << '\n';
return os;
}
现在,您可以通过非常简单的方式看到提示用户输入 bookno,units_sold
和价格
读取
和<$的方式c $ c> print 函数的工作方式以及该函数之一中发生的错误如何更改流状态。简短的 main()
可以是:
Now you can see in a very simple way prompting the user to enter a bookno, units_sold
and price
how both the read
and print
functions work, as well as how an error occurring within one of the function changes the stream state. A short main()
could be:
int main (void) {
Sales_data sd;
std::cout << "enter bookno units_sold price: ";
if (read (std::cin, sd)) /* if read succeeds */
print (std::cout, sd); /* print data */
else
std::cerr << "error: invalid input\n";
}
示例用法/输出
$ ./bin/readprintstream
enter bookno units_sold price: 12 100 12.95
units sold: 100 revenue: $1295
或如果发生错误:
$ ./bin/readprintstream
enter bookno units_sold price: 23 banannas 12.28
error: invalid input
希望讨论和示例有助于使概念更清晰。如果您还有其他问题,请告诉我。
Hopefully the discussion and example helps clear the concept up a bit. If you have further questions, let me know.
这篇关于使用istream参数读取的istream函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!