std::ostream and std::istream are the standard library classes for output and input. Objects such as std::cout are output streams, and std::cin is an input stream. The string stream classes in <sstream> are built on the same types, so the operators you learn with cout and cin also work on text in memory.
What ostream is for
An std::ostream is a stream you write to. The << operator sends values into it, and each << returns the stream itself, so you can chain them. This function takes any output stream by reference:
#include <iostream>
#include <sstream>
#include <string>
void print_pair(std::ostream& os, int a, int b) {
os << "(" << a << ", " << b << ")";
}
int main() {
print_pair(std::cout, 3, 4);
std::cout << '\n';
std::ostringstream buffer;
print_pair(buffer, 5, 6);
std::cout << buffer.str() << '\n';
}
The output is:
(3, 4)
(5, 6)
Because the function takes std::ostream&, it works with std::cout, with an std::ostringstream, and with a file stream.
What istream is for
An std::istream is a stream you read from. The >> operator pulls values out of it, and std::getline pulls whole lines.
#include <iostream>
#include <sstream>
int read_sum(std::istream& is) {
int total = 0;
int value = 0;
while (is >> value) {
total += value;
}
return total;
}
int main() {
std::istringstream in("1 2 3 4");
std::cout << read_sum(in) << '\n'; // 10
}
If you passed std::cin to read_sum instead, it would keep adding numbers until the input ends or a value that is not a number appears.
How the classes fit together
The classes form a small family tree:
std::iosholds the state that every stream shares.std::ostreamandstd::istreamboth derive fromstd::ios.std::iostreamderives from bothstd::ostreamandstd::istream.std::ostringstreamandstd::ofstreamderive fromstd::ostream.std::istringstreamandstd::ifstreamderive fromstd::istream.std::stringstreamandstd::fstreamderive fromstd::iostream.
You do not need to memorize the tree. The useful point is that a function written for std::ostream& also accepts std::cout, a file stream, and an std::ostringstream.
Stream state
A stream tracks its condition with flags:
good()is true when no error flag is set.eof()is true after the stream has reached the end of its input.fail()is true after an operation failed, such as a read that found no valid number.bad()is true after a more serious, unrecoverable error.
if (in) checks whether the stream is still usable. It is the same as checking that fail() is false. After a failure, the stream stays in that state until you call clear(), which is the same behavior you see with std::cin.
Streams cannot be copied
A stream holds state, so it cannot be copied. Pass one to a function by reference, using std::ostream& or std::istream&. Passing a stream by value does not compile.
Common mistakes
- Passing a stream by value. Take it by reference instead, as shown above.
- Using
std::endlon every line.std::endlwrites a newline and flushes the stream, which can slow down heavy output.'\n'writes only the newline, and it is usually the better choice for plain line breaks. - Taking the wrong direction. A function that takes
std::istream&can read from the stream but not write to it. Usestd::ostream&for output, orstd::iostream&when it needs both.
Summary
std::ostream is for writing and std::istream is for reading. cout and cin are the familiar examples, and the string stream classes use the same types. Take streams by reference, and remember that clear() resets a stream after a failure.