- iostream
-
C++ Standard Library Standard Template Library C++11 array
forward_list
unordered_map
unordered_set
C Standard Library - Data types
- Character classification
- Strings
- Mathematics
- File input/output
- Date/time
- Localization
- Memory allocation
- Program control
- Miscellaneous headers:
iostream
is a header file which is used for input/output in the C++ programming language. It is part of the C++ standard library. The name stands for Input/Output Stream. In C++ and its predecessor, the C programming language, there is no special syntax for streaming data input or output. Instead, these are combined as a library of functions. Like thecstdio
header inherited from C's stdio.h,iostream
provides basic input and output services for C++ programs. iostream uses the objectscin
,cout
,cerr
, andclog
for sending data to and from the standard streams input, output, error (unbuffered), and error (buffered) respectively. As part of the C++ standard library, these objects are a part of thestd
namespace.Contents
Example usage
The canonical Hello world program can be expressed as follows:
#include <iostream> int main() { std::cout << "Hello,world!\n"; return 0; }
This program would output "Hello, world!" followed by a newline and standard output stream buffer flush.
The
cout
object is of typeostream
, which overloads the left bit-shift operator to make it perform an operation completely unrelated to bitwise operations. Thecerr
andclog
objects are also of typeostream
, so they overload that operator as well. Thecin
object is of typeistream
, which overloads the right bit-shift operator. The directions of the bit-shift operators make it seem as though data is flowing towards the output stream or flowing away from the input stream.An alternative to the newline character
\n
isendl
, which is used as follows:#include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }
endl
is an output manipulator that writes a newline and flushes the buffer, ensuring that the data is output immediately. Several other manipulators are listed below.Output formatting
Methods
width(int x)
minimum number of characters for next output fill(char x)
character used to fill with in the case that the width needs to be elongated to fill the minimum. precision(int x)
sets the number of significant digits for floating-point numbers Example:
using namespace std; cout.width(10); cout << "ten" << "four" << "four";
Manipulators
Manipulators are objects that can modify a stream using the
<<
or>>
operators.endl
"end line": inserts a newline into the stream and calls flush. ends
"end string": inserts a null character into the stream and calls flush. flush
forces an output stream to write any buffered characters dec
changes the output format of number to be in decimal format oct
changes the output format of number to be in octal format hex
changes the output format of number to be in hexadecimal format ws
causes an inputstream to 'eat' whitespace showpoint
tells the stream to show the decimal point and some zeros with whole numbers Other manipulators can be found using the header
iomanip
.Criticism
Some environments do not provide a shared implementation of the C++ library. These include embedded systems and Windows systems running programs built with MinGW. Under these systems, the C++ standard library must be statically linked to a program, which increases the size of the program,[1] or distributed as a shared library alongside the program. Some implementations of the C++ standard library have significant amounts of dead code. For example, GNU libstdc++ automatically constructs a locale when building an
ostream
even if a program never uses any types (date, time or money) that a locale affects,[2] and a statically-linked hello world program that uses<iostream>
of GNU libstdc++ produces an executable an order of magnitude larger than an equivalent program that uses<cstdio>
.[3] There exist partial implementations of the C++ standard library designed for space-constrained environments; their<iostream>
may leave out features that programs in such environments may not need, such as locale support.[4]Naming conventions
Please refer to Standard streams.
References
- ^ MinGW.org: Large executables. Accessed April 22, 2009.
- ^ GNU libstdc++ source code,
bits/ios_base.h
- ^ Pin Eight: RAnT (Rants, Articles, and Treatises)
- ^ uClibc++ C++ library
External links
Categories:- C++ Standard Library
- Articles with example C++ code
Wikimedia Foundation. 2010.