Copyright (C) 1993 Free Software Foundation, Inc.

The author of this software is David M. Gay.

Copyright (c) 1991 by AT&T.

Permission to use, copy, modify, and distribute this software for any purpose without fee is hereby granted, provided that this entire notice is included in all copies of any software which is or includes a copy or modification of this software and in all copies of the supporting documentation for such software.

THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.


2 Operators and Default Streams

The STL port library,  implements the standard input and output facilities for C++. These facilities are roughly analogous with those defined by the C `stdio' functions.

Although these definitions come from a library, rather than being part of the "core language", they are sufficiently central to be specified in the latest working papers for C++.

The user can use two operators defined in this library for basic input and output operations. They are familiar from any C++ introductory textbook: << for output, and >> for input. These operators are often used in conjunction with three streams that are open by default:

Variable: ostream cout
The standard output stream, analogous to the C stdout.
 
Variable: istream cin
The standard input stream, analogous to the C stdin.

Variable: ostream cerr
An alternative output stream for errors, analogous to the C stderr.

For example, this bare-bones C++ version of the traditional "hello" program uses << and cout:

#include <iostream.h>

int main(int argc, char **argv)
{
  cout << "Well, hi there.\n";
  return 0;
}

Robust code checks the state of the input and output streams between operations (for example, using the method good). For more information see 3.1.1 Checking the state of a stream. The user may need to adjust the maximum input or output field widths, using manipulators like setw, or setprecision.

Operator: ostream <<

Writes output to an open output stream of class ostream defined by this library on any object of a C++ primitive type, and on other classes of the library. The user caYou can overload the definition for any of your own applications' classes.

Returns a reference to the implied argument *this (the open stream it writes on), permitting statements like

cout << "The value of i is " << i << "\n";

Operator: istream >>

Read input from an open input stream of class istream. Defined by this library on primitive numeric, pointer, and string types; you can extend the definition for any of your own applications' classes.

Returns a reference to the implied argument *this (the open stream it reads), permitting multiple inputs in one statement.