C++ 中的流操纵器(Stream Manipulators)

C++ 中的流操纵器(Stream Manipulators)是非常有用的工具,用于控制输入和输出流的格式和行为。下面是一些常见的流操纵器及其详细说明和使用示例。

常见流操纵器及其功能

1. setw
  • 功能:设置输出字段的宽度。
  • 用法std::setw(width)
  • 注意:只影响紧接着的下一个输出项。
  • 示例
    #include <iostream>
    #include <iomanip> // for setw
    
    int main() {
        std::cout << "[" << std::setw(10) << "Hello" << "]" << std::endl;
        return 0;
    }
    
    输出[ Hello]
2. setfill
  • 功能:设置用于填充空白位置的字符。
  • 用法std::setfill(char)
  • 示例
    #include <iostream>
    #include <iomanip> // for setfill and setw
    
    int main() {
        std::cout << "[" << std::setfill('-') << std::setw(10) << "Hello" << "]" << std::endl;
        return 0;
    }
    
    输出[-----Hello]
3. leftright
  • 功能:设置左对齐或右对齐。
  • 用法std::leftstd::right
  • 示例
    #include <iostream>
    #include <iomanip> // for left, right and setw
    
    int main() {
        std::cout << "[" << std::left << std::setw(10) << "Hello" << "]" << std::endl;
        std::cout << "[" << std::right << std::setw(10) << "Hello" << "]" << std::endl;
        return 0;
    }
    
    输出
    [Hello     ]
    [     Hello]
    
4. boolalphanoboolalpha
  • 功能:控制布尔值的输出格式。
    • boolalpha:以文字形式输出布尔值(true/false)。
    • noboolalpha:以数字形式输出布尔值(1/0)。
  • 用法std::boolalphastd::noboolalpha
  • 示例
    #include <iostream>
    
    int main() {
        std::cout << std::boolalpha << true << " " << false << std::endl;
        std::cout << std::noboolalpha << true << " " << false << std::endl;
        return 0;
    }
    
    输出
    true false
    1 0
    
5. showbasenoshowbase
  • 功能:控制整数基数前缀的显示。
    • showbase:显示整数的基数前缀(如 0x 表示十六进制,0 表示八进制)。
    • noshowbase:不显示基数前缀。
  • 用法std::showbasestd::noshowbase
  • 示例
    #include <iostream>
    #include <iomanip> // for showbase and hex
    
    int main() {
        int num = 255;
        std::cout << std::showbase << std::hex << num << std::endl;
        std::cout << std::noshowbase << num << std::endl;
        return 0;
    }
    
    输出
    0xff
    ff
    
6. uppercasenouppercase
  • 功能:控制十六进制数和科学计数法的字母大小写。
    • uppercase:使用大写字母。
    • nouppercase:使用小写字母。
  • 用法std::uppercasestd::nouppercase
  • 示例
    #include <iostream>
    #include <iomanip> // for uppercase and hex
    
    int main() {
        int num = 255;
        std::cout << std::uppercase << std::hex << num << std::endl;
        std::cout << std::nouppercase << num << std::endl;
        return 0;
    }
    
    输出
    FF
    ff
    
7. hex, dec, 和 oct
  • 功能:设置整数的基数。
    • hex:十六进制。
    • dec:十进制。
    • oct:八进制。
  • 用法std::hex, std::dec, std::oct
  • 示例
    #include <iostream>
    #include <iomanip> // for hex, dec, oct
    
    int main() {
        int num = 255;
        std::cout << std::hex << num << std::endl;
        std::cout << std::dec << num << std::endl;
        std::cout << std::oct << num << std::endl;
        return 0;
    }
    
    输出
    ff
    255
    377
    
8. fixedscientific
  • 功能:控制浮点数的显示格式。
    • fixed:定点格式。
    • scientific:科学计数法格式。
  • 用法std::fixedstd::scientific
  • 示例
    #include <iostream>
    #include <iomanip> // for fixed and scientific
    
    int main() {
        double num = 123.456789;
        std::cout << std::fixed << num << std::endl;
        std::cout << std::scientific << num << std::endl;
        return 0;
    }
    
    输出
    123.456789
    1.234568e+02
    
9. setprecision
  • 功能:设置浮点数的精度。
  • 用法std::setprecision(n),其中 n 是精度位数。
  • 示例
    #include <iostream>
    #include <iomanip> // for setprecision
    
    int main() {
        double num = 123.456789;
        std::cout << std::fixed << std::setprecision(2) << num << std::endl;
        std::cout << std::setprecision(5) << num << std::endl;
        return 0;
    }
    
    输出
    123.46
    123.45679
    

综合示例

以下是一个综合示例,展示如何结合使用这些流操纵器:

#include <iostream>
#include <iomanip>

int main() {
    int num = 255;
    double pi = 3.141592653589793;

    // 设置宽度、填充字符和对齐方式
    std::cout << "[" << std::setw(10) << std::left << num << "]" << std::endl;
    std::cout << "[" << std::setw(10) << std::right << num << "]" << std::endl;
    std::cout << "[" << std::setw(10) << std::setfill('-') << num << "]" << std::endl;

    // 设置整数的基数和显示格式
    std::cout << std::showbase << std::hex << num << std::endl;
    std::cout << std::dec << num << std::endl;
    std::cout << std::oct << num << std::endl;

    // 设置浮点数的显示格式和精度
    std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
    std::cout << std::scientific << std::setprecision(5) << pi << std::endl;

    // 布尔值的显示格式
    std::cout << std::boolalpha << true << " " << false << std::endl;
    std::cout << std::noboolalpha << true << " " << false << std::endl;

    return 0;
}

运行结果

[255       ]
[       255]
[-------255]
0xff
255
0377
3.14
3.14159e+00
true false
1 0
  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值