C++ 中的 Manipulators
C++ 中的 manipulators 是与插入 (<<) 和提取 (>>) 操作符 (cin/cout) 一起使用的 特殊函数或对象,用于控制输入和输出流。您可以更改输出的显示格式或输入的读取方式。这些 manipulators 在 <iostream> 和 <iomanip> 头文件中声明。
C++ 中的 Manipulators 类型
manipulators 可以分为以下类别 −
- Output Stream Manipulators
- Input Stream Manipulators
- Alignment Manipulators
- Floating Point Manipulators
- Numeric Base and Case Manipulators
- Boolean Manipulators
- Time and Date Manipulators
Output Stream Manipulators
output stream manipulators 用于 控制显示属性,如字段宽度、填充字符和数字显示选项。以下列出了所有 output stream manipulators −
| Manipulators | 定义 | 语法 |
|---|---|---|
| endl | 它插入一个 换行符 并 刷新 输出缓冲区。 | cout << "Tutorials" << endl; |
| setw(n) | 它为下一个输出操作 设置字段宽度。 | cout << setw(5) << 57; |
| setfill() | 它设置 填充字符。 | cout << setfill('*') << setw(5) << 42; |
| showpoint | 它为浮点数显示 小数点。 | cout << showpoint << 7.0; |
| noshowpoint | 它隐藏小数点。 | cout << noshowpoint << 7.0; |
| showpos | 它用于为正数显示 '+' 符号。 | cout << showpos << 57; |
| noshowpos | 它为正数隐藏 '+' 符号。 | cout << noshowpos << 57; |
| flush | 它在不换行的前提下 刷新 输出流。 | cout << "Data" << flush; |
| ends | 它用于插入一个空字符然后刷新。 | cout << "String" << ends; |
以下是一个 示例,演示了 C++ 中一些 output stream manipulators 的使用 −
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout << "C++ Output Manipulators Examples:" << endl;
cout << "\nThis is the first line" << endl;
cout << "This is the second line using endl" << endl;
cout << endl;
cout << setw(5) << 57
<< " (Field width 5) using setw()" << endl;
cout << setw(10) << 40
<< " (Field width 10) using setw()" << endl;
cout << "\n";
cout << setfill('*') << setw(8) << 42
<< " Filled with * using setfill()" << endl;
cout << setfill('-') << setw(8) << 123
<< " Filled with - using setfill()" << endl;
cout << setfill(' '); // 重置为空格
cout << "\nDecimal Point Display:" << endl;
cout << "With showpoint: " << showpoint << 7.0
<< ", " << 3.0 << endl;
cout << "With noshowpoint: " << noshowpoint << 7.0
<< ", " << 3.0 << endl;
return 0;
}
上述代码的 输出 如下 −
C++ Output Manipulators Examples:
This is the first line
This is the second line using endl
57 (Field width 5) using setw()
40 (Field width 10) using setw()
******42 Filled with * using setfill()
-----123 Filled with - using setfill()
Decimal Point Display:
With showpoint: 7.00000, 3.00000
With noshowpoint: 7, 3
输入流操作符
输入操作符控制空白字符,并且在读取操作期间处理输入流。
| 操作符 | 定义 | 语法 |
|---|---|---|
| ws | 它移除空白字符。 | cin >> ws >> str; |
| noskipws | 它避免跳过空白字符。 | cin >> noskipws >> ch; |
以下示例展示了输入流操作符ws和noskipws的用法 −
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string text = " A B";
cout << "Original Text: " << text << endl;
istringstream iss(text);
char ch;
iss >> ws >> ch;
cout << "Using ws: '" << ch << "'" << endl;
// 重置流
iss.clear();
iss.seekg(0);
iss >> noskipws >> ch;
cout << "Using noskipws: '" << ch << "'" << endl;
return 0;
}
上述代码的输出如下。在此,ws跳过所有空白字符,而noskipws打印空白字符而不跳过它。
Original Text: A B Using ws: 'A' Using noskipws: ' '
对齐操作符
对齐操作符用于在字段宽度内控制输出的对齐方式。
| 操作符 | 定义 | 语法 |
|---|---|---|
| left | 它在字段宽度内左对齐输出。 | cout << left << setw(10) << "Hi"; |
| right | 它在字段宽度内右对齐输出。 | cout << right << setw(10) << "Hi"; |
| internal | 它用于在符号和数值之间插入填充。 | cout << internal << setw(6) << -57; |
在这个示例中,我们使用了left和right操作符,分别将数字左对齐和右对齐,而internal则设置'-'和数字之间的对齐。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num = 123;
int negNum = -123;
cout << "Left aligned : '" << left << setw(5)
<< num << "'" << endl;
cout << "Right aligned : '" << right << setw(5)
<< num << "'" << endl;
cout << "Internal align : '" << internal << setw(5)
<< negNum << "'" << endl;
return 0;
}
上述代码的输出如下:
Left aligned : '123 ' Right aligned : ' 123' Internal align : '- 123'
浮点数操作符
浮点数操作符用于控制小数精度并设置表示小数的记法。
| 操作符 | 定义 | 语法 |
|---|---|---|
| setprecision(n) | 它设置小数输出的精度。指定输出到小数点后多少位。 | cout << setprecision(2) << 1.41421356; |
| fixed | 它以定点记法表示给定的数字。 | cout << fixed << 1.41421356; |
| scientific | 它以科学记法表示给定的数字。 | cout << scientific << 1234.5; |
以下示例使用setprecision()设置给定数字的精度。如果不使用setprecision()操作符,fixed和scientific会给出6位小数的结果。
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double num = 1.41421356;
cout << "Original number:" << num << endl;
cout << "Default: " << setprecision(5) << num
<< endl;
cout << "Fixed: " << fixed << setprecision(3)
<< num << endl;
cout << "Scientific: " << scientific << setprecision(3)
<< num << endl;
return 0;
}
上述代码的输出如下:
Original number:1.41421 Default: 1.4142 Fixed: 1.414 Scientific: 1.414e+00
进制和大小写操纵器
进制操纵器用于转换和设置给定数字的进制。大小写操纵器指定您希望将十六进制和科学计数法输出为大写或小写。
| 操纵器 | 定义 | 语法 |
|---|---|---|
| dec | 它将给定值设置为decimal output。 | cout << dec << 57; |
| oct | 它将给定值设置为octal output。 | cout << oct << 57; |
| hex | 它将给定值设置为hexadecimal output。 | cout << hex << 57; |
| setbase | 用于在输出中将数字进制设置为 decimal、octal 或 hexadecimal。 | cout << setbase(16) << 42; |
| showbase | 用于在输出中显示进制前缀。 | cout << showbase << hex << 42; |
| noshowbase | 它在输出中隐藏进制前缀。 | cout << noshowbase << hex << 42; |
| uppercase | 它将 hex 和 scientific 表示为大写字母。 | cout << uppercase << hex << 255; |
| nouppercase | 它将 hex 和 scientific 表示为小写字母。 | cout << nouppercase << hex << 255; |
以下示例演示了进制和大小写操纵器的使用,其中进制操纵器将给定数字的进制设置为或转换为 decimal、hex 或 oct。大小写操纵器将给定值转换为小写和大写。
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int number = 255;
cout << "Original number:" << number << endl;
cout << "Decimal: " << dec << number << ", "
<< "Octal: " << oct << number << ", "
<< "Hex: " << hex << number << endl;
// 重置为 decimal
cout << dec;
cout << "\nSetting base using setbase():" << endl;
cout << "Base 10: " << setbase(10) << number << ", "
<< "Base 8: " << setbase(8) << number << ", "
<< "Base 16: " << setbase(16) << number << endl;
cout << "\nHiding base prefix using noshowbase:" << endl;
cout << noshowbase;
cout << "Decimal: " << dec << number << ", "
<< "Octal: " << oct << number << ", "
<< "Hex: " << hex << number << endl;
cout << "\nDisplaying base prefix using showbase:" << endl;
cout << showbase;
cout << "Decimal: " << dec << number << ", "
<< "Octal: " << oct << number << ", "
<< "Hex: " << hex << number << endl;
cout << "\nLowercase hex using nouppercase: " << nouppercase
<< hex << showbase << 255 << ", " << 171 << endl;
cout << "Uppercase hex uppercase: " << uppercase << hex
<< showbase << 255 << ", " << 171 << endl;
return 0;
}
上述代码的输出如下所示:
Original number:255 Decimal: 255, Octal: 377, Hex: ff Setting base using setbase(): Base 10: 255, Base 8: 377, Base 16: ff Hiding base prefix using noshowbase: Decimal: 255, Octal: 377, Hex: ff Displaying base prefix using showbase: Decimal: 255, Octal: 0377, Hex: 0xff Lowercase hex using nouppercase: 0xff, 0xab Uppercase hex uppercase: 0XFF, 0XAB
布尔值操纵器
Boolean manipulators 用于将布尔值表示为 true/false 或 0/1。
| 操纵器 | 定义 | 语法 |
|---|---|---|
| boolalpha | 它将布尔值显示为 true/false。 | cout << boolalpha << true; |
| noboolalpha | 它将布尔值显示为 1/0。 | cout << noboolalpha << true; |
以下是一个示例,演示了 boolean manipulators 的使用,其中 boolalpha 和 noboolalpha 分别将布尔值显示为 true/false 和 1/0。
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout << "使用 boolalpha 进行比较" << endl;
int a = 10, b = 20, c = 10;
cout << boolalpha;
cout << "a = " << a << ", b = " << b << ", c = "
<< c << endl;
cout << "a == b: " << (a == b) << ", "
<< "a == c: " << (a == c) << ", "
<< "a < b: " << (a < b) << ", "
<< "b > c: " << (b > c) << endl;
cout << "\n使用 noboolalpha 进行比较" << endl;
cout << noboolalpha;
cout << "a = " << a << ", b = " << b << ", c = "
<< c << endl;
cout << "a == b: " << (a == b) << ", "
<< "a == c: " << (a == c) << ", "
<< "a < b: " << (a < b) << ", "
<< "b > c: " << (b > c) << endl;
return 0;
}
上述代码的输出如下所示:
使用 boolalpha 进行比较 a = 10, b = 20, c = 10 a == b: false, a == c: true, a < b: true, b > c: true 使用 noboolalpha 进行比较 a = 10, b = 20, c = 10 a == b: 0, a == c: 1, a < b: 1, b > c: 1
时间和日期操纵器
你可以使用时间和日期操纵器将时间格式化为指定格式,或者解析格式化的时间和日期。
| 操纵器 | 定义 | 语法 |
|---|---|---|
| put_time | 用于按照指定格式格式化和输出时间。 | cout << put_time(&tm, "%Y-%m-%d"); |
| get_time | 用于解析格式化的时间输入。 | cin >> get_time(&tm, "%Y-%m-%d"); |
以下示例演示了时间和日期操纵器,分别使用 put_time 和 get_time 来格式化和解析日期和时间:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <ctime>
using namespace std;
int main(){
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "使用 put_time 显示当前日期和时间: " << "\n";
cout << put_time(ltm, "%Y-%m-%d %H:%M:%S") << endl;
string dateStr = "2025-09-08 14:30:00";
tm t = {};
istringstream ss(dateStr);
cout << "使用 get_time 显示自定义日期和时间: " << "\n"
<< dateStr << endl;
ss >> get_time(&t, "%Y-%m-%d %H:%M:%S");
return 0;
}
上述代码的输出如下所示:
使用 put_time 显示当前日期和时间: 2025-09-08 07:19:03 使用 get_time 显示自定义日期和时间: 2025-09-08 14:30:00
结论
C++ 中的操纵器是与输入输出流(cin 和 cout)一起使用的特殊函数,用于更改输出格式和读取输入的方式。C++ 中的操纵器可以进一步分为以下类型:output stream manipulators、input stream manipulators、alignment manipulators、floating point manipulators、numeric base and case manipulators、boolean manipulators、time and date manipulators。