C++ cout 对象
示例
使用 cout
对象输出不同数据类型
string myStr = "Hello World!";
bool myBool = false;
int myInt = 50;
float myFloat = 19.99;
cout << myStr << "\n";
cout << myBool << "\n";
cout << myInt << "\n";
cout << myFloat << "\n";
定义和用法
The cout
object is used to output values/print text.
The cout
object is used to output values/print text.
cout << "Hello World!";
The insertion operator can be used more than once on the same line to output multiple values
cout << "The answer is: " << x;
注意: cout
对象定义在 <iostream>
头文件中。
操作符
Manipulators allow you to change the formatting of the output. They are used with the <<
insertion operator in the same way as literal values and variables, and they affect output that follows them.
除了 setw()
,操作符的效果会一直保持,直到另一个操作符改变它。
The table below shows a list of useful manipulators
操作符 | 描述 | 示例 |
---|---|---|
boolalpha |
Displays boolean values as "true" and "false" instead of "1" and "0". | cout << boolalpha << false; |
dec |
将整数表示为十进制数字。 | cout << dec << 12; |
endl |
Outputs a newline character. This manipulator also flushes the output buffer which makes it less efficient than printing \n . |
cout << "Line 1" << endl << "Line 2"; |
ends |
Outputs the null terminating character used to end C-style strings. Mainly used when writing into files. | cout << "Hello World!" << ends; |
fixed |
用固定的小数位数表示浮点数。小数位数可以使用 setprecision() 操作符来确定。 |
cout << fixed << 19.99; |
hex |
将整数表示为十六进制数字。 | cout << hex << 12; |
internal |
如果指定了宽度(使用 setw() 操作符),数字的符号将左对齐,而值将右对齐,其他数据类型的输出将右对齐。 |
cout << setw(10) << internal << -12345; |
left |
如果指定了宽度(使用 setw() 操作符),则将输出左对齐。 |
cout << setw(10) << left << "Hello"; |
noboolalpha |
用于重置由 boolalpha 操作符所做的更改。 |
cout << noboolalpha << false; |
noshowbase |
用于重置由 showbase 操作符所做的更改。 |
cout << hex << noshowbase << 12; |
noshowpoint |
用于重置由 showpoint 操作符所做的更改。 |
cout << noshowpoint << 12345.0; |
noshowpos |
用于重置由 showpos 操作符所做的更改。 |
cout << noshowpos << 12; |
nouppercase |
用于重置由 uppercase 操作符所做的更改。 |
cout << hex << nouppercase << 12; |
oct |
将整数表示为八进制数字。 | cout << oct << 12; |
right |
如果指定了宽度(使用 setw() 操作符),则将输出右对齐。 |
cout << setw(10) << right << "Hello"; |
fixed |
用科学记数法表示浮点数。小数位数可以使用 setprecision() 操作符来确定。 |
cout << fixed << 19.99; |
setfill() |
选择一个字符用作填充。 需要 <iomanip> 库。 |
cout << setfill('.') << setw(10) << 19.99; |
setprecision() |
选择浮点数的精度。如果使用了 fixed 或 scientific 操作符,它指定小数位数,否则它指定有效数字的数量。需要 <iomanip> 库。 |
cout << setprecision(4) << 12.3456; |
setw() |
指定下一个输出的最小字符宽度。如果输出不够宽,则添加填充以填充剩余空间。 需要 <iomanip> 库。 |
cout << setw(10) << "Hello"; |
showbase |
将整数表示为十六进制或八进制时,在数字前加上 "0x" 或 "0" 以显示其基数。 | cout << hex << showbase << 12; |
showpoint |
Always displays the decimal point for floating point numbers even if it is not needed. | cout << showpoint << 12345.0; |
showpos |
Always displays a + sign next to positive numbers. | cout << showpos << 12; |
uppercase |
用大写字母表示十六进制数字和科学记数法中的 "e"。 | cout << hex << uppercase << 12; |
示例
Use manipulators to change how output is formatted
// Booleans
cout << "Booleans\n";
cout << false << "\n";
cout << boolalpha << false << "\n";
// Hexadecimal and octal numbers
cout << "\nHexadecimal and octal numbers\n";
int myInt = 14;
cout << dec << myInt << "\n";
cout << hex << myInt << "\n";
cout << oct << myInt << "\n";
cout << showbase << uppercase;
cout << hex << myInt << "\n";
cout << oct << myInt << "\n";
cout << dec;
// Floating point numbers
cout << "\nFloating point numbers\n";
float myFloat = 19.99;
cout << myFloat << "\n";
cout << showpos << showpoint << 12345.0 << "\n";
cout << noshowpos << noshowpoint;
cout << fixed << myFloat << "\n";
cout << scientific << myFloat << "\n";
// Alignment
cout << "\nAlignment\n";
cout << setw(10) << left << "Left" << "\n";
cout << setw(10) << right << "Right" << "\n";
cout << setw(10) << internal << -12345 << " (Internal)\n";
方法
The cout
object also has methods which can do the same operations as the <<
insertion operator.
Output methods
The cout.write(str, n)
method outputs the first n characters from the char
array str without any formatting.
The cout.put(c)
method outputs the specified character c
without any formatting.
Formatting methods
The cout.precision(p)
method specifies how many digits are used to represent floating point numbers. By default it specifies the number of significant digits to display. If the ios::fixed
or ios::scientific
flag is enabled then it specifies how many digits follow the decimal point.
The cout.width(w)
method specifies the minimum number of characters wide the next output should occupy. If the output does not have enough characters then padding characters will be added to fill up the remaining space. By default the padding characters are spaces and they are added to the left so that the content is aligned to the right. The alignment can be changed by using one of the ios::adjustfield
flags described in the Flags section below.
示例
cout.width(10);
cout << 5 << "\n";
cout.width(10);
cout << 25 << "\n";
cout.width(10);
cout << 125 << "\n";
The cout.fill(c)
method specifies which character will be used as padding.
示例
cout.fill('.');
cout.width(10);
cout << 5 << "\n";
cout.width(10);
cout << 25 << "\n";
cout.width(10);
cout << 125 << "\n";
Flags
The setf()
and unsetf()
methods are used to set or unset flags which change the formatting of the output. There are a variety of different flags. Some flags belong to a group and, in that case, the setf()
method should have a second parameter specifying which group it belongs to so that the other flags of the group can be reset.
Flag | 语法 | 描述 |
---|---|---|
ios::boolalpha |
cout.setf(ios::boolalpha) |
Displays boolean values as "true" and "false" instead of "1" and "0". |
ios::showbase |
cout.setf(ios::showbase) |
将整数表示为十六进制或八进制时,在数字前加上 "0x" 或 "0" 以显示其基数。 |
ios::showpoint |
cout.setf(ios::showpoint) |
Always displays the decimal point for floating point numbers even if it is not needed. |
ios::showpos |
cout.setf(ios::showpoint) |
Always displays a + sign next to positive numbers. |
ios::uppercase |
cout.setf(ios::uppercase) |
用大写字母表示十六进制数字和科学记数法中的 "e"。 |
ios::dec |
cout.setf(ios::dec, ios::basefield) |
Represents integers as decimal digits. Belongs to the ios::basefield group. |
ios::hex |
cout.setf(ios::hex, ios::basefield) |
Represents integers as hexadecimal digits. Belongs to the ios::basefield group. |
ios::oct |
cout.setf(ios::oct, ios::basefield) |
Represents integers as octal digits. Belongs to the ios::basefield group. |
ios::fixed |
cout.setf(ios::fixed, ios::floatfield) |
Represents floating point numbers with a fixed number of decimal places. The number of decimal places can be established with the cout.precision() method. Belongs to the ios::floatfield group. |
ios::scientific |
cout.setf(ios::scientific, ios::floatfield) |
Represents floating point numbers in scientific notation. The number of decimal places can be established with the cout.precision() method. Belongs to the ios::floatfield group. |
ios::internal |
cout.setf(ios::internal, ios::adjustfield) |
If a width is specified, for numbers the sign is left-aligned while the value is right-aligned, for other data types the output is aligned to the right. Belongs to the ios::adjustfield group. |
ios::left |
cout.setf(ios::left, ios::adjustfield) |
Aligns output to the left when a width is specified. Belongs to the ios::adjustfield group. |
ios::right |
cout.setf(ios::right, ios::adjustfield) |
Aligns output to the right when a width is specified. Belongs to the ios::adjustfield group. |
示例
Use flags to change how output is formatted
// Booleans
cout << "Booleans\n";
cout << false << "\n";
cout.setf(ios::boolalpha);
cout << false << "\n";
// Hexadecimal and octal numbers
cout << "\nHexadecimal and octal numbers\n";
int myInt = 14;
cout << myInt << "\n";
cout.setf(ios::hex, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::oct, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::showbase);
cout.setf(ios::uppercase);
cout.setf(ios::hex, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::oct, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::dec, ios::basefield);
// Floating point numbers
cout << "\nFloating point numbers\n";
float myFloat = 19.99;
cout << myFloat << "\n";
cout.setf(ios::fixed, ios::floatfield);
cout << myFloat << "\n";
cout.setf(ios::scientific, ios::floatfield);
cout << myFloat << "\n";
cout.unsetf(ios::floatfield);
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout << 12345.0 << "\n";
// Alignment
cout << "\nAlignment\n";
cout.setf(ios::left, ios::adjustfield);
cout << setw(10) << "Left" << "\n";
cout.setf(ios::right, ios::adjustfield);
cout << setw(10) << "Right" << "\n";
cout.setf(ios::internal, ios::adjustfield);
cout << setw(10) << 12345.0 << " (Internal)\n";