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

Use the insertion (<<) and extraction (>>) operators defined in this library for basic input and output operations. 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 World" program uses << and cout:

#include <iostream.h>

int main(int argc, char **argv)
{
  cout << "Hello World!\n";
  return 0;
}

Although, the use of these operators is all right in throwaway code, it is not good programming practice. A robust code checks the state of the input and output streams between operations (for example, using the method good) see Checking the state of a stream. To adjust maximum input or output field widths, use 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 definition can be overloaded for any of the user's own application 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 >>

Reads input from an open input stream of class istream. Defined by this library on primitive numeric, pointer, and string types; the definition can be expanded for any of the user's own application classes.

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