There are two basic parts in the iostream stream Library of C + +. respectively:
 
1. Flow: C + + regards input and output as byte throttling. When input, the program extracts bytes from the output stream, and the program inserts bytes into the output stream when it is output. A stream acts as a bridge between a program and a stream source or flow target.
 
2. Buffer: Buffer is a block of memory used as a mediator, a temporary storage tool that transfers information from the device to a program or from a program to a device to match the speed gap between programs and devices. From the design, added the buffer, so that the C + + iostream structure is more extensible.
 
C + + input and Output class diagram:
 
 
The following introductions to the various streaming classes in C + + are mainly from wikis and site Cplusplus.
 
1.1 Ios_base
 
The Ios_base class encapsulates the flow in the C + + standard and defines the basic information and behavior of data types that are not dependent on read-write in input and output, such as formatting information, exception status, event callbacks, and so on.
 
In class Std::ios_base, the following information about the flow is saved:
 
The enumeration type of the format control information fmtflags, which affects how to interpret the format of the input serial, how to generate the output serial format, such as whether the integer is 16 or 10, and whether the floating-point number is a scientific notation or a fixed-point form;
 
The State enumeration type of the stream is iostate, such as whether the data is complete, whether it reaches the end of the stream, whether read or write fails, etc.
 
How the stream is opened enumeration type OpenMode, such as read, write, append, delete original content when created, binary open,
 
The location of the stream is enumerated type Seekdir, such as the start position, current position, end position, and so on.
 
Event enumeration type events for the stream, such as "erase" event erase_event, change locale set event imbue_event, copy Format event copyfmt_event.
 
The private additional data of the stream, a long array with an array of pointers.
 
A member class failure, used as the base class for various exceptions thrown by the stream input output class library as a C + + standard.
 
A member class init that encapsulates initialization functions for 8 static objects, such as cout, Cin, and Wcout.
 
member functions include:
 
Format:
 
1. Read/Set the format of the stream
 
[CPP]
 
Fmtflags flags () const;
 
Fmtflags flags (fmtflags FMTFL);
 
Fmtflags flags () const;
 
Fmtflags flags (fmtflags FMTFL); Example:
 
[CPP]
 
Modify Flags
 
#include//Std::cout, Std::ios
 
int main () {
 
Std::cout.flags (std::ios::right | std::ios::hex | std::ios::showbase);
 
Std::cout.width (10);
 
Std::cout << << ' \ n ';
 
return 0;
 
}
 
Modify Flags
 
#include//Std::cout, Std::ios
 
int main () {
 
Std::cout.flags (std::ios::right | std::ios::hex | std::ios::showbase);
 
Std::cout.width (10);
 
Std::cout << << ' \ n ';
 
return 0;
 
}
 
2. Set the format of the stream, merged with the original format
 
[CPP]
 
Fmtflags setf (Fmtflags FMTFL);
 
Fmtflags setf (fmtflags FMTFL, fmtflags mask);
 
Fmtflags setf (Fmtflags FMTFL);
 
Fmtflags setf (fmtflags FMTFL, fmtflags mask); Example:
 
[CPP]
 
modifying flags with SETF/UNSETF
 
#include//Std::cout, Std::ios
 
int main () {
 
STD::COUT.SETF (Std::ios::hex, Std::ios::basefield); Set Hex as the Basefield
 
STD::COUT.SETF (std::ios::showbase); Activate Showbase
 
Std::cout << << ' \ n ';
 
STD::COUT.UNSETF (std::ios::showbase); Deactivate Showbase
 
Std::cout << << ' \ n ';
 
return 0;
 
}
 
modifying flags with SETF/UNSETF
 
#include//Std::cout, Std::ios
 
int main () {
 
STD::COUT.SETF (Std::ios::hex, Std::ios::basefield); Set Hex as the Basefield
 
STD::COUT.SETF (std::ios::showbase); Activate Showbase
 
Std::cout << << ' \ n ';
 
STD::COUT.UNSETF (std::ios::showbase); Deactivate Showbase
 
Std::cout << << ' \ n ';
 
return 0;
 
Output
 
Output:
 
0x64
 
64
 
3. Some bits (bit) to clear the format of the stream according to the parameter mask
 
[CPP]
 
void Unsetf (Fmtflags mask);
 
void Unsetf (Fmtflags mask); Example:
 
[CPP]
 
modifying flags with SETF/UNSETF
 
#include//Std::cout, Std::ios
 
int main () {
 
STD::COUT.SETF (Std::ios::hex, Std::ios::basefield); Set Hex as the Basefield
 
STD::COUT.SETF (std::ios::showbase); Activate Showbase
 
Std::cout << << ' \ n ';
 
STD::COUT.UNSETF (std::ios::showbase); Deactivate Showbase
 
Std::cout << << ' \ n ';
 
return 0;
 
}
 
modifying flags with SETF/UNSETF
 
#include//Std::cout, Std::ios
 
int main () {
 
STD::COUT.SETF (Std::ios::hex, Std::ios::basefield); Set Hex as the Basefield
 
STD::COUT.SETF (std::ios::showbase); Activate Showbase
 
Std::cout << << ' \ n ';
 
STD::COUT.UNSETF (std::ios::showbase); Deactivate Showbase
 
Std::cout << << ' \ n ';
 
return 0;
 
Output
 
0x64
 
64
 
4. Read/set accuracy when displaying floating point numbers
 
[CPP]
 
Streamsize precision () const;
 
Streamsize Precision (streamsize prec);
 
Streamsize precision () const;
 
Streamsize Precision (streamsize prec); Example:
 
[CPP]
 
Modify Precision
 
#include//Std::cout, Std::ios
 
int main () {
 
Double F = 3.14159;
 
STD::COUT.UNSETF (Std::ios::floatfield); Floatfield not set
 
Std::cout.precision (5);
 
Std::cout << f << ' \ n ';
 
Std::cout.precision (10);
 
Std::cout << f << ' \ n ';
 
STD::COUT.SETF (std::ios::fixed, std:: Ios::floatfield); Floatfield set to Fixed
 
Std::cout << f << ' \ n ';
 
return 0;
 
}
 
Modify Precision
 
#include//Std::cout, Std::ios
 
int main () {
 
Double F = 3.14159;
 
STD::COUT.UNSETF (Std::ios::floatfield); Floatfield not set
 
Std::cout.precision (5);
 
Std::cout << f << ' \ n ';
 
Std::cout.precision (10);
 
Std::cout << f << ' \ n ';
 
STD::COUT.SETF (std::ios::fixed, std:: Ios::floatfield); Floatfield set to Fixed
 
Std::cout << f << ' \ n ';
 
return 0;
 
Output
 
3.1416
 
3.14159
 
3.1415900000
 
5. The display width of the output data of the read/set stream
 
[CPP]
 
Streamsize width () const;
 
Streamsize width (streamsize wide);
 
Streamsize width () const;
 
Streamsize width (streamsize wide); Example:
 
[CPP]
 
Field width
 
#include//Std::cout, Std::left
 
int main () {
 
Std::cout << << ' \ n ';
 
Std::cout.width (10);
 
Std::cout << << ' \ n ';
 
Std::cout.fill (' x ');
 
Std::cout.width (15);
 
Std::cout << std::left << << ' \ n ';
 
return 0;
 
}
 
Field width
 
#include//Std::cout, Std::left
 
int main () {
 
Std::cout << << ' \ n ';
 
Std::cout.width (10);
 
Std::cout << << ' \ n ';
 
Std::cout.fill (' x ');
 
Std::cout.width (15);
 
Std::cout << std::left << << ' \ n ';
 
return 0;
 
Output
 
100
 
100
 
100xxxxxxxxxxxx
 
Language environment:
 
1. Set the local locale for the stream
 
[CPP]
 
Locale Imbue (const locale& LOC);
 
Locale Imbue (const locale& LOC); Example:
 
[CPP]
 
Imbue Example
 
#include//Std::cout
 
#include//Std::locale
 
int main ()
 
{
 
Std::locale Mylocale (""); Get global Locale
 
Std::cout.imbue (Mylocale); Imbue Global Locale
 
Std::cout << 3.14159 << ' \ n ';
 
return 0;
 
}
 
Imbue Example
 
#include//Std::cout
 
#include//Std::locale
 
int main ()
 
{
 
Std::locale Mylocale (""); Get global Locale
 
Std::cout.imbue (Mylocale); Imbue Global Locale
 
Std::cout << 3.14159 << ' \ n ';
 
return 0;
 
Output