C++ 中的流操纵器(Stream Manipulators)是非常有用的工具,用于控制输入和输出流的格式和行为。下面是一些常见的流操纵器及其详细说明和使用示例。
常见流操纵器及其功能
1. setw
- 功能:设置输出字段的宽度。
- 用法:
std::setw(width)
- 注意:只影响紧接着的下一个输出项。
- 示例:
输出:#include <iostream> #include <iomanip> // for setw int main() { std::cout << "[" << std::setw(10) << "Hello" << "]" << std::endl; return 0; }
[ Hello]
2. setfill
- 功能:设置用于填充空白位置的字符。
- 用法:
std::setfill(char)
- 示例:
输出:#include <iostream> #include <iomanip> // for setfill and setw int main() { std::cout << "[" << std::setfill('-') << std::setw(10) << "Hello" << "]" << std::endl; return 0; }
[-----Hello]
3. left
和 right
- 功能:设置左对齐或右对齐。
- 用法:
std::left
或std::right
- 示例:
输出:#include <iostream> #include <iomanip> // for left, right and setw int main() { std::cout << "[" << std::left << std::setw(10) << "Hello" << "]" << std::endl; std::cout << "[" << std::right << std::setw(10) << "Hello" << "]" << std::endl; return 0; }
[Hello ] [ Hello]
4. boolalpha
和 noboolalpha
- 功能:控制布尔值的输出格式。
boolalpha
:以文字形式输出布尔值(true/false)。noboolalpha
:以数字形式输出布尔值(1/0)。
- 用法:
std::boolalpha
或std::noboolalpha
- 示例:
输出:#include <iostream> int main() { std::cout << std::boolalpha << true << " " << false << std::endl; std::cout << std::noboolalpha << true << " " << false << std::endl; return 0; }
true false 1 0
5. showbase
和 noshowbase
- 功能:控制整数基数前缀的显示。
showbase
:显示整数的基数前缀(如 0x 表示十六进制,0 表示八进制)。noshowbase
:不显示基数前缀。
- 用法:
std::showbase
或std::noshowbase
- 示例:
输出:#include <iostream> #include <iomanip> // for showbase and hex int main() { int num = 255; std::cout << std::showbase << std::hex << num << std::endl; std::cout << std::noshowbase << num << std::endl; return 0; }
0xff ff
6. uppercase
和 nouppercase
- 功能:控制十六进制数和科学计数法的字母大小写。
uppercase
:使用大写字母。nouppercase
:使用小写字母。
- 用法:
std::uppercase
或std::nouppercase
- 示例:
输出:#include <iostream> #include <iomanip> // for uppercase and hex int main() { int num = 255; std::cout << std::uppercase << std::hex << num << std::endl; std::cout << std::nouppercase << num << std::endl; return 0; }
FF ff
7. hex
, dec
, 和 oct
- 功能:设置整数的基数。
hex
:十六进制。dec
:十进制。oct
:八进制。
- 用法:
std::hex
,std::dec
,std::oct
- 示例:
输出:#include <iostream> #include <iomanip> // for hex, dec, oct int main() { int num = 255; std::cout << std::hex << num << std::endl; std::cout << std::dec << num << std::endl; std::cout << std::oct << num << std::endl; return 0; }
ff 255 377
8. fixed
和 scientific
- 功能:控制浮点数的显示格式。
fixed
:定点格式。scientific
:科学计数法格式。
- 用法:
std::fixed
或std::scientific
- 示例:
输出:#include <iostream> #include <iomanip> // for fixed and scientific int main() { double num = 123.456789; std::cout << std::fixed << num << std::endl; std::cout << std::scientific << num << std::endl; return 0; }
123.456789 1.234568e+02
9. setprecision
- 功能:设置浮点数的精度。
- 用法:
std::setprecision(n)
,其中n
是精度位数。 - 示例:
输出:#include <iostream> #include <iomanip> // for setprecision int main() { double num = 123.456789; std::cout << std::fixed << std::setprecision(2) << num << std::endl; std::cout << std::setprecision(5) << num << std::endl; return 0; }
123.46 123.45679
综合示例
以下是一个综合示例,展示如何结合使用这些流操纵器:
#include <iostream>
#include <iomanip>
int main() {
int num = 255;
double pi = 3.141592653589793;
// 设置宽度、填充字符和对齐方式
std::cout << "[" << std::setw(10) << std::left << num << "]" << std::endl;
std::cout << "[" << std::setw(10) << std::right << num << "]" << std::endl;
std::cout << "[" << std::setw(10) << std::setfill('-') << num << "]" << std::endl;
// 设置整数的基数和显示格式
std::cout << std::showbase << std::hex << num << std::endl;
std::cout << std::dec << num << std::endl;
std::cout << std::oct << num << std::endl;
// 设置浮点数的显示格式和精度
std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
std::cout << std::scientific << std::setprecision(5) << pi << std::endl;
// 布尔值的显示格式
std::cout << std::boolalpha << true << " " << false << std::endl;
std::cout << std::noboolalpha << true << " " << false << std::endl;
return 0;
}
运行结果
[255 ]
[ 255]
[-------255]
0xff
255
0377
3.14
3.14159e+00
true false
1 0