简体   繁体   English

C ++ <istream> 和 <ostream>

[英]C++ <istream> and <ostream>

Hi I am currently studying c++ from a beginners book. 嗨,我目前正在从初学者的书中学习c ++。 In the book the author gives a brief explanation of both header files istream and ostream . 在书中,作者简要解释了头文件istreamostream Unfortunately I don't quite understand what he means. 不幸的是,我不太明白他的意思。 I have tried to look them up online but it doesn't help me understand his explanation. 我试图在网上查找它们,但它无法帮助我理解他的解释。

He says 他说

istream : Contains the extractors for inputting data from streams and includes the template class basic_istream . istream :包含用于从流输入数据的提取器,包括模板类basic_istream In other words, istream puts the I in I/O. 换句话说, istream将I放入I / O.

ostream : Contains the inserters for outputting a series of bytes and includes the template basic_istream . ostream :包含用于输出一系列字节的插入器,并包含模板basic_istream basically ostream puts the O in I/O. 基本上ostream将O放入I / O.

What I don't understand is why do you need extractors for inputting data from streams and vice-versa for the ostream . 我不明白的是为什么你需要提取,用于从流,反之亦然输入数据ostream

Data that serves as input to your program must be extracted from the istream that provides it. 必须从提供它的istream提取作为程序输入的数据。

Likewise, data that serves as output from your program must be inserted into the ostream that spirits it away. 同样,必须将用作程序输出的数据插入到使其消失的ostream中。

+------------------+                  +-----------------------------------------+
| DATA SOURCE      |  ----input---->  | [istream] --extractor-->  YOUR PROGRAM  |
+------------------+                  +-----------------------------------------+

+------------------+                  +-----------------------------------------+
| DATA SINK        |  <---output----  | [ostream] <--inserter---  YOUR PROGRAM  |
+------------------+                  +-----------------------------------------+

What I don't understand is why do you need extractors for inputting data from streams and vice-versa for the ostream . 我不明白的是为什么你需要提取,用于从流,反之亦然输入数据ostream

Say you have istream_obj as input data stream. 假设您将istream_obj作为输入数据流。 Now you write: 现在你写:

int x;
istream_obj>>x;

Here istream will try to extract an int from istream_obj with the overload like: 这里istream将尝试从istream_obj提取一个带有重载的int,如:

void operator >> (istream _stream, int x); // may not be exactly this but similar thing will be done.

This is what extraction from an istream means 这是从istream中提取的意思

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM