C++基础 - IO格式化

  标准库定义了一组操纵符来修改流的格式状态,一个操纵符是一个函数或是一个对象,会影响流的状态,当操纵符改变流的格式状态时,通常改变后的状态对所有后续IO都生效。也有例外,比如endl。

控制布尔值格式

    // 控制布尔值格式
    bool truev = true;
    bool falsev = false;
    cout << truev << " , " << falsev << endl;
    cout << boolalpha << truev << " , " << falsev << endl;
    cout << noboolalpha << truev << " , " << falsev << endl;

指定整形值的进制,浮点数表示不受影响

    cout << "default: " << 20 << " , " << 1024 << endl;
    cout << "oct: " << oct << 20 << " , " << 1024 << endl; // 八进制
    cout << "hex: " << hex << 20 << " , " << 1024 << endl; // 十六进制
    cout << "dec: " << dec << 20 << " , " << 1024 << endl << endl; // 十进制

在输出中指出进制,字符以大写的形式打印

    cout << showbase << uppercase;
    cout << "default: " << 20 << " , " << 1024 << endl;
    cout << "oct: " << oct << 20 << " , " << 1024 << endl; // 八进制
    cout << "hex: " << hex << 20 << " , " << 1024 << endl; // 十六进制
    cout << "dec: " << dec << 20 << " , " << 1024 << endl; // 十进制
    cout << noshowbase << nouppercase << endl;

控制浮点数格式

1)指定打印精度(多少个数字)
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl;
    cout.precision(12); // 成员函数precision, 有参版本,设置精度,无参版本,返回当前精度
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl;
    cout << setprecision(3); // 操纵符
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl;
    cout << setprecision(6) << endl; // 恢复默认6位精度

2)指定浮点数计数法
    cout << "default format: " << 100 * sqrt(2.0) << endl; // 默认模式,精度控制数字的总位数
    cout << "scientific: " << scientific << 100 * sqrt(2.0) << endl; // 科学计数法
    cout << "fixed decimal: " << fixed << 100 * sqrt(2.0) << endl; // 定点十进制
    cout << "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << endl; // 十六进制
    cout << "defaultfloat: " << defaultfloat << 100 * sqrt(2.0) << endl << endl; // 恢复默认模式

3)打印小数点
    cout << 10.0 << endl; // 默认,当一个浮点数的小数部分为0时,不显示小数点,showpoint强制打印小数点
    cout << showpoint << 10.0 << endl;
    cout << noshowpoint << 10.0 << endl << endl;

输出补白

    int i = -16;
    double d = 3.14159;
    // 默认右对齐
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 左对齐
    cout << left;
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 右对齐
    cout << right;
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 中间,控制负号的位置
    cout << internal;
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 设置补白字符
    cout << setfill('#');
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

控制输入格式

    cin >> noskipws; // 设置输入运算符读取空白符(空格符, 制表符, 换行符,回车符)
    cin >> skipws; // 恢复到默认状态

参考

C++ primer中文版 第五版 第17章

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值