简体   繁体   中英

C++ <istream> and <ostream>

Hi I am currently studying c++ from a beginners book. In the book the author gives a brief explanation of both header files istream and ostream . 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 . In other words, istream puts the I in I/O.

ostream : Contains the inserters for outputting a series of bytes and includes the template basic_istream . basically ostream puts the O in I/O.

What I don't understand is why do you need extractors for inputting data from streams and vice-versa for the ostream .

Data that serves as input to your program must be extracted from the istream that provides it.

Likewise, data that serves as output from your program must be inserted into the ostream that spirits it away.

+------------------+                  +-----------------------------------------+
| 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 .

Say you have istream_obj as input data stream. Now you write:

int x;
istream_obj>>x;

Here istream will try to extract an int from istream_obj with the overload like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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