C++ Standard Library |
---|
Containers |
C standard library |
|
char
.std
namespace, and the main header changed from <iostream.h>
to <iostream>
. It is this standardized version that is covered in the rest of the article.basic_fstream<CharT,Traits>
refers to the generic class template that implements input/output operations on file streams. It is usually used as fstream
which is an alias for basic_fstream<char,char_traits<char>>
, or, in other words, basic_fstream
working on characters of type char
with the default character operation set.Class | Explanation | Typedefs |
---|---|---|
Stream buffers (low level functionality) | ||
basic_streambuf | provides abstract low level input/output interface, that can be implemented for concrete data sources or sinks. Rarely used directly. |
|
basic_filebuf | implements low level input/output interface for file-based streams. Rarely used directly. |
|
basic_stringbuf | implements low level input/output interface for string-based streams. Rarely used directly. |
|
Support classes | ||
ios_base | manages formatting information and exception state | N/A |
basic_ios | manages a stream buffer |
|
Input streams buffers (high level functionality) | ||
basic_istream | wraps an abstract stream buffer and provides high level input interface, such as formatting capabilities. |
|
basic_ifstream | an input stream that wraps a file stream buffer. Provides functions to open or close a file in addition to those of generic input stream |
|
basic_istringstream | an input stream that wraps a string stream buffer. Provides functions to access the underlying string in addition to those of generic input stream |
|
Output streams buffers (high level functionality) | ||
basic_ostream | wraps an abstract stream buffer and provides high level output interface, such as formatting capabilities. |
|
basic_ofstream | an output stream that wraps a file stream buffer. Provides functions to open or close a file in addition to those of generic output stream |
|
basic_ostringstream | an output stream that wraps a string stream buffer. Provides functions to access the underlying string in addition to those of generic output stream |
|
Input/output streams buffers (high level functionality) | ||
basic_iostream | wraps an abstract stream buffer and provides high level input/output interface, such as formatting capabilities. |
|
basic_fstream | an input/output stream that wraps a file stream buffer. Provides functions to open or close a file in addition to those of generic input/output stream |
|
basic_stringstream | an input/output stream that wraps a string stream buffer. Provides functions to access the underlying string in addition to those of generic input/output stream |
|
<ios>
contains the definitions of ios_base
and basic_ios
classes, that manage formatting information and the associated stream-buffer.<istream>
contains the definition of basic_istream
class template, which implements formatted input.<ostream>
contains the definition of basic_ostream
class template, which implements formatted output.<iostream>
contains the definition of basic_iostream
class template, which implements formatted input and output, and includes <ios>
, <istream>
and <ostream>
.<fstream>
contains the definitions of basic_ifstream
, basic_ofstream
and basic_fstream
class templates which implement formatted input, output and input/output on file streams.<sstream>
contains the definitions of basic_istringstream
, basic_ostringstream
and basic_stringstream
class templates which implement formatted input, output and input/output on string-based streams.<iomanip>
contains formatting manipulators.<iosfwd>
contains forward declarations of all classes in the input/output library.ios_base
and basic_ios
are two classes that manage the lower-level bits of a stream. ios_base
stores formatting information and the state of the stream. basic_ios
manages the associated stream-buffer. basic_ios
is commonly known as simply ios
or wios
, which are two typedefs for basic_ios
with a specific character type. basic_ios
and ios_base
are very rarely used directly by programmers. Usually, their functionality is accessed through other classes such as iostream
which inherit them.[6][7]Name | description |
---|---|
ios | convenience typedef for a basic_ios working with characters of type char |
wios | convenience typedef for a basic_ios working with characters of type wchar_t |
streamoff | supports internal operations. |
streampos | holds the current position of the buffer pointer or file pointer. |
wstreampos | holds the current position of the buffer pointer or file pointer. |
streamsize | specifies the size of the stream. |
Name | Description |
---|---|
boolalpha / noboolalpha | specifies whether variables of type bool appear as true and false or as 0 and 1 in the stream. |
skipws / noskipws | specifies whether the white space is skipped in input operations |
showbase / noshowbase | specifies whether the notational base of the number is displayed |
showpoint / noshowpoint | specifies whether to display the fractional part of a floating point number, when the fractional part is zero |
showpos / noshowpos | specifies whether to display + for positive numbers |
unitbuf / nounitbuf | specifies whether the output should be buffered |
uppercase / nouppercase | specifies whether uppercase characters should be used in hexadecimal integer and floating-point output |
left / right / internal | specifies how a number should be justified |
dec / oct / hex | specifies the notation an integer number should be displayed in |
fixed / scientific /hexfloat (C++11) / defaultfloat (C++11) | specifies the notation a floating-point number should be displayed in |
iostream
, a header file that 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 the cstdio
header inherited from C's stdio.h, iostream
provides basic input and output services for C++ programs. iostream uses the objectscin
, cout
, cerr
, and clog
for sending data to and from the standard streams input, output, error (unbuffered), and log (buffered) respectively. As part of the C++ standard library, these objects are a part of the std
namespace.[8]cout
object is of type ostream
, which overloads the left bit-shiftoperator to make it perform an operation completely unrelated to bitwise operations, and notably evaluate to the value of the left argument, allowing multiple operations on the same ostream object, essentially as a different syntax for method cascading, exposing a fluent interface. The cerr
and clog
objects are also of type ostream
, so they overload that operator as well. The cin
object is of type istream
, 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.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 |
<<
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 |
ws | causes an inputstream to 'eat' whitespace |
showpoint | tells the stream to show the decimal point and some zeros with whole numbers |
iomanip
.ostream
even if a program never uses any types (date, time or money) that a locale affects,[10]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>
.[11]<iostream>
may leave out features that programs in such environments may not need, such as locale support.[12]..endl, which flushes the output buffer and sends a newline to the standard output stream.
bits/ios_base.h