输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
2018-02-25 23:36
330 查看
一、以操纵子方式格式化
数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符。把它们作为插入操作符<<的输出对象即可。如setiosflags、setw、setfill、setprecision、hex、oct等。
(一)、常用的流操纵算子:
(二)、ios类的枚举常量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <iostream> #include <iomanip> using namespace std; // 通过操纵子方式进行格式化输出 // 宽度控制 // 对齐控制 // 填充控制 // 精度控制 // 进制输出 int main(void) { //system("chcp 936"); int n = 64; double d = 123.45; double d2 = 0.0187; cout << "=================宽度控制=====================" << endl; cout << n << '#' << endl; cout << setw(10) << n << '#' << n << endl; // 宽度控制不会影响下一个输出 cout << "=================对齐控制=====================" << endl; cout << setw(10) << setiosflags(ios::left) << n << '#' << endl; cout << setw(10) << n << '#' << endl; // 对齐控制会影响下一个输出 //cout<<setw(10)<<setiosflags(ios::right)<<n<<'#'<<endl; cout << setw(10) << resetiosflags(ios::left) << n << '#' << endl; //去除左对齐 cout << "=================填充控制=====================" << endl; cout << setw(10) << setfill('?') << n << '#' << endl; cout << setw(10) << n << '#' << endl; // 填充控制会影响下一个输出 cout << setw(10) << setfill(' ') << n << '#' << endl; cout << "=================精度控制=====================" << endl; cout << setprecision(4) << d << endl; //有效数字 cout << setprecision(2) << d2 << endl; cout << setiosflags(ios::fixed); cout << setprecision(4) << d << endl; // 小数点后面位数 cout << setprecision(2) << d2 << endl; cout << "=================进制输出=====================" << endl; cout << n << endl; cout << oct << n << endl; cout << hex << n << endl; cout << endl; cout << setiosflags(ios::showbase); //八进制加前缀0,十六进制加前缀0x cout << dec << n << endl; cout << oct << n << endl; cout << hex << n << endl; cout << endl; cout << setbase(10) << n << endl; //八进制加前缀0,十六进制加前缀0x cout << setbase(8) << n << endl; cout << setbase(16) << n << endl; return 0; } |
二、以类成员函数方式格式化
通过调用流的成员函数控制格式,如setf、unsetf、width、fill、precision等。优点是在设置格式同时,可以返回以前的设置,便于恢复原来的设置。
ios类提供成员函数对流的状态进行检测和进行输入输出格式控制等操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#include <iostream> //#include <iomanip> using namespace std; // 通过成员函数方式进行格式化输出 // 宽度控制 // 对齐控制 // 填充控制 // 精度控制 // 进制输出 int main(void) { //system("chcp 936"); int n = 64; double d = 123.45; double d2 = 0.0187; cout << "=================宽度控制=====================" << endl; cout << n << '#' << endl; cout.width(10); cout << n << '#' << n << endl; // 宽度控制不会影响下一个输出 cout << "=================对齐控制=====================" << endl; cout.width(10); cout.setf(ios::left); cout << n << '#' << endl; cout.width(10); cout << n << '#' << endl; // 对齐控制会影响下一个输出 //cout.width(10); //cout.setf(ios::right); //cout<<n<<'#'<<endl; cout.width(10); cout.unsetf(ios::left); cout << n << '#' << endl; cout << "=================填充控制=====================" << endl; cout.width(10); cout.fill('?'); cout << n << '#' << endl; cout.width(10); cout << n << '#' << endl; // 填充控制会影响下一个输出 cout.width(10); cout.fill(' '); cout << n << '#' << endl; cout << "=================精度控制=====================" << endl; cout.precision(4); cout << d << endl; cout.precision(2); cout << d2 << endl; cout.setf(ios::fixed); cout.precision(4); cout << d << endl; cout.precision(2); cout << d2 << endl;; cout << "=================进制输出=====================" << endl; cout.setf(ios::showbase); cout << n << endl; cout.unsetf(ios::dec); cout.setf(ios::oct); cout << n << endl; cout.unsetf(ios::oct); cout.setf(ios::hex); cout << n << endl; return 0; } |
参考:
C++ primer 第四版
Effective C++ 3rd
C++编程规范
相关文章推荐
- 从零开始学C++之IO流类库(四):输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
- 从零开始学C++之IO流类库(四):输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
- 从零开始学C++之IO流类库(四):输出流格式化(以操纵子方式格式化 以ios类成员函数方式格式化)
- 使用ios的成员函数进行格式化输入和输出
- C++用ios类的成员函数实现格式化I/O范例
- printf函数常用的输出方式及格式化输出说明
- C,C++--用IOS成员格式化输入和输出
- 如何输出类的非静态成员函数地址
- 格式化输出函数(3): FormatFloat
- iOS中格式化输出日志
- 格式化输出函数printf
- 方便的格式化OutputDebugString输出函数
- printf函数十六进制格式化输出并且2字节对齐
- iOS 在类实现定义中声明成员变量的怪异方式
- IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog输出,NSLog两种不同情况的输出方式
- C语言中printf格式化输出函数
- C语言中printf格式化输出函数
- Delphi格式化输出函数(1): Format
- (14)Linux_C_格式化输人输出函数
- 类的成员函数存储方式(是否属于类的对象)