C++--iomanip库

目录

1. 设置字段宽度:std::setw()

2. 设置浮点数精度:std::setprecision()

3. 设置填充字符:std::setfill()

4. 控制对齐方式:std::left 和 std::right,std::internal

5. 控制进制输出:std::hex、std::dec、std::oct

6. 显示进制前缀:std::showbase 、大写输出:std::uppercase

7. 控制布尔值输出格式:std::boolalpha

8. 显示正号+:std::showpos

9. 针对浮点型:std::fixed,std::scientific

10. 解除设置,恢复默认格式:std::resetiosflags()

11. 设置读取字符数:setw()

12. 保存与恢复流状态:std::ios::fmtflags


          <iomanip> 是 C++ 标准库中的一个头文件,用于控制输入输出的格式。它提供了一些流操作符(manipulators),可以帮助你更方便地格式化输出,比如设置宽度、精度、填充字符等。

        以下是 `<iomanip>` 中一些常用的操作符:

1. 设置字段宽度:std::setw()

        - 用于设置输出的宽度。

     #include <iostream>
     #include <iomanip>
     int main() {
         std::cout << std::setw(10) << 123 << std::endl; // 输出宽度为10,右对齐
         return 0;
     }

     输出:
     ```
           123
     ```

2. 设置浮点数精度:std::setprecision()

        - 用于设置浮点数输出的精度(小数点后的位数)。

     #include <iostream>
     #include <iomanip>
     int main() {
         double pi = 3.1415926535;
         std::cout << std::setprecision(4) << pi << std::endl; // 输出3.142
         return 0;
     }

3. 设置填充字符:std::setfill()

        - 用于设置填充字符,通常与std::setw 一起使用。

    #include <iostream>
     #include <iomanip>
     int main() {
         std::cout << std::setw(10) << std::setfill('*') << 123 << std::endl; 
         // 输出*******123
         return 0;
     }

4. 控制对齐方式:std::left 和 std::right,std::internal

        - std::left:左对齐。

        - std::right:右对齐(默认)。

        - std::internal:针对数值右对齐,如针对-123,可对齐为-****123。

     #include <iostream>
     #include <iomanip>
     int main() {
         std::cout << std::left << std::setw(10) << 123 << std::endl;  // 左对齐
         std::cout << std::right << std::setw(10) << 123 << std::endl; // 右对齐
         return 0;
     }
    #include <iostream>
    #include <iomanip>
    int main() {
        int num = -123;
        std::cout << std::setw(8) << std::internal << std::setfill('*') << num << std::endl; // 输出 -***123
        return 0;
    }

5. 控制进制输出:std::hex、std::dec、std::oct

        - std::hex:输出十六进制。

        - std::dec:输出十进制(默认)。

        - std::oct:输出八进制。


     #include <iostream>
     #include <iomanip>
     int main() {
         int num = 255;
         std::cout << std::hex << num << std::endl; // 输出ff
         std::cout << std::dec << num << std::endl; // 输出255
         std::cout << std::oct << num << std::endl; // 输出377
         return 0;
     }

6. 显示进制前缀:std::showbase 、大写输出:std::uppercase

    #include <iostream>
    #include <iomanip>
    int main() {
        int num = 255;
        std::cout << std::showbase << std::uppercase << std::hex << num << std::endl; 
        // 输出 0XFF
        return 0;
    }

7. 控制布尔值输出格式:std::boolalpha

        - 将布尔值输出为 true/false,而不是 1/0。

     #include <iostream>
     int main() {
         bool flag = true;
         std::cout << std::boolalpha << flag << std::endl; // 输出true
         return 0;
     }

8. 显示正号+:std::showpos

        - showpos 强制正数输出时显示正号。

        代码示例如8图.

9. 针对浮点型:std::fixed,std::scientific

        - fixed 针对浮点型变量以固定的小数位数进行输出的设置。(又称定点表示法)

        - scientific 针对浮点型变量以科学计数法的形式输出。

    #include <iostream>
    #include <iomanip>
    int main() {
        double num = 123.456;
        // 强制显示正号,定点表示法,保留两位小数
        std::cout << std::showpos << std::fixed << std::setprecision(2) << num << std::endl; 
        // 输出 +123.46
        return 0;
    }
    #include <iostream>
    #include <iomanip>
    int main() {
        double num = 123.456789;
        std::cout << std::fixed << std::setprecision(3) << num << std::endl;      
        // 123.457
        std::cout << std::scientific << std::setprecision(3) << num << std::endl; 
        // 1.235e+02
        return 0;
    }

         科学计数法:编程中的科学计数法

        另外,有fixed参与的设置(如上的 std::showpos << std::fixed << std::setprecision(2))还会作用于以后的所有代码,所以需要搭配 std::resetiosflags(std::ios::fixed) 来解除。

10. 解除设置,恢复默认格式:std::resetiosflags()

        - 恢复默认格式。

    #include <iostream>
    #include <cstdio>
    #include <iomanip>
    using namespace std;

    int main()
    {
        int n = 7;
        double m = 3.1415926;   //重点关注 m 的输出变化
        cout << fixed << setprecision(2) << n << endl;  //7
        cout << m << endl;  //3.14
        cout << n << endl;  //7
        cout << resetiosflags(ios::fixed) << n << endl; //7
        cout << m << endl;  //3.1415926
    }

11. 设置读取字符数:setw()

         - 限制读取的字符数。

    #include <iostream>
    #include <iomanip>
    int main() {
        char buffer[10];
        std::cout << "输入一个单词(最多4字符):";
        std::cin >> std::setw(5) >> buffer; // 防止缓冲区溢出
        std::cout << "读取的内容:" << buffer << std::endl;
        return 0;
    }

12. 保存与恢复流状态:std::ios::fmtflags

        - 这是个数据类型,用于修改流后,对这修改方案的保存。

    #include <iostream>
    #include <iomanip>
    int main() {
        std::ios::fmtflags original_flags = std::cout.flags(); // 保存原始状态
        std::cout << std::hex << std::showbase << 255 << std::endl; // 输出 0xff
        std::cout.flags(original_flags); // 恢复状态
        std::cout << 255 << std::endl;   // 输出 255(十进制)
        return 0;
    }

        其成员函数有三,

        - flags,用于获取当前的输出流格式,

std::ios::fmtflags flags = std::cout.flags(); 

        - setf(...),用于设定格式标志,如hex、showbase等,

std::cout.setf(std::ios_base::hex, std::ios_base::basefield); std::cout.setf(std::ios_base::showbase);

        - unsetf(),用于清除指定格式标志,重置为默认值,

    #include <iostream>

    int main() {
        // 设置格式标志
        std::cout.setf(std::ios::hex, std::ios::basefield);
        std::cout.setf(std::ios::showbase);

        // 输出一个整数
        std::cout << 42 << std::endl;

        // 关闭设置的格式标志
        std::cout.unsetf(std::ios::hex);
        std::cout.unsetf(std::ios::showbase);

        return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值