一、以操纵子方式格式化

数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符。把它们作为插入操作符<<的输出对象即可。如setiosflags、setw、setfill、setprecision、hex、oct等。

(一)、常用的流操纵算子:

(二)、ios类的枚举常量

 C++ Code 
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类提供成员函数对流的状态进行检测和进行输入输出格式控制等操作:

 C++ Code 
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++编程规范

输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)的更多相关文章

  1. 从零开始学C++之IO流类库(四):输出流格式化(以操纵子方式格式化 以ios类成员函数方式格式化)

    一.以操纵子方式格式化 数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符.把它们作为插入操作符<<的输出对象即可.如setiosflags.setw.set ...

  2. C++运算符重载(成员函数方式)

    一.运算符重载 C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型,也需要有类似的运算操作.如果将C++中这些现存的运算符直接作用于用户自定义的类型数据上,会得到什么样 ...

  3. c++成员函数的存储方式---11

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 成员函数属于一个类的成员,出现再类体中.可以被指定为公有,私有或受保护的. 1.在类外面定义成员函数时, ...

  4. 【转】C++成员函数的存储方式

    [转] http://c.biancheng.net/cpp/biancheng/view/187.html 用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据 ...

  5. C++对象模型的那些事儿之六:成员函数调用方式

    前言 C++的成员函数分为静态函数.非静态函数和虚函数三种,在本系列文章中,多处提到static和non-static不影响对象占用的内存,而虚函数需要引入虚指针,所以需要调整对象的内存布局.既然已经 ...

  6. C++成员函数在内存中的存储方式

    用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码分 ...

  7. C++运算符重载(友元函数方式)

    我们知道,C++中的运算符重载有两种形式:①重载为类的成员函数(见C++运算符重载(成员函数方式)),②重载为类的友元函数. 当重载友元函数时,将没有隐含的参数this指针.这样,对双目运算符,友元函 ...

  8. C++成员函数的存储方式

    用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间. 按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码 ...

  9. C++ 类的存储方式以及虚函数表

    一.C++成员函数在内存中的存储方式 用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就 ...

随机推荐

  1. Mastering stack and heap for system reliability

    http://www.iar.com/Global/Resources/Developers_Toolbox/Building_and_debugging/Mastering_stack_and_he ...

  2. SMT Surface Mount Technology footprint references

    http://en.wikipedia.org/wiki/Surface-mount_technology Surface-mount technology (SMT) is a method for ...

  3. springBoot-自定义监听器

    package com.cx.springboot.mylistener; import org.springframework.boot.context.event.ApplicationReady ...

  4. Labeled Faces in the Wild 人脸识别数据集 部分测试数据

    development test set Note: images displayed are original (non-aligned/funneled) images. match pairs ...

  5. Centos 7 配置 VNCServer 經驗

    安裝 Centos 7後, 習慣性的安裝  Xmanager 3或4, 都不能正常工作, 無奈之下開始安裝 VNCServer. (個人習慣使用Xmanager, 因為不需要安裝,只要配置一下就能用, ...

  6. Centos7 安装pycharm

    可以通过浏览器访问http://www.jetbrains.com/pycharm/,选择需要下载的版本,也可以通过wget下载可执行程序. [root@localhost ~] wget https ...

  7. 在 Windows 上快速安装并运行 Laravel 5.x

    安装 PHP 注意一:Laravel 5.0 开始对 PHP 版本的要求是 >=5.4,Laravel 5.1 要求 PHP 版本>=5.5.9,所以,建议大家尽量安装 5.5.x 的最新 ...

  8. 【Web】Rest && 权限管理 && LDAP && OAuth && Nginx && Lua 等

    最好的8个 Java RESTful 框架:http://www.importnew.com/17138.html 如何设计RESTful的API权限:https://segmentfault.com ...

  9. 关于DrawIndexedPrimitive函数的调用

    函数的原型例如以下所看到的: HRESULT DrawIndexedPrimitive( [in] D3DPRIMITIVETYPE Type, [in] INT BaseVertexIndex, [ ...

  10. 截短字符串的函数(JS中适用)

    function cutShort(str){    if(str.length>15){        str=str.substr(0,15)+"...";    }   ...