小白C++入坑学废之旅(十)

前面不正经地学习了一些c++的知识,这一篇正经地学习一下c++关于IO方面的一些小知识。在c++的标准库文件里,我没有找到类似Java一样比较好用和强大的IO处理。当然c++也有一套行之有效的处理原则,这也算是意外之喜。

这里不先啰嗦c++的iostream, fstream或者stringstream等,而是介绍一下IO辅助操作的一些小玩意。比如如何输出十六进制数值,又比如如何控制浮点数的精度等。

进制表示

我们都知道在计算机中数值的表示方式有很多种,我们耳熟能详的通常是二进制,十进制,八进制以及十六进制这四种。那么如何读取或者输出这些格式的数值呢。就需要用到std::octstd::decstd::hex等操作符。比如我们要输出某个数值的八进制,十进制以及十六进制的表示值就可以这么做:

int number = 16;

    std::cout << "The number in octal: " << std::oct << number << std::endl;
    std::cout << "The number in decimal: " << std::dec << number << std::endl;
    std::cout << "The number in hex: " << std::hex << number << std::endl;

然后我们就可以得到这样的结果:

The number in octal: 20

The number in decimal: 16

The number in hex: 10

貌似这样的结果和我们想象里的不太一样。我们通常认为的八进制或则十六进制数值都是0或者0x打头。别着急,我们还需要std::showbase的帮忙:

std::cout << "The number in octal with prefix: " << std::oct << std::showbase << number << std::endl;
    std::cout << "The number in decimal with prefix: " << std::dec << std::showbase << number << std::endl;
    std::cout << "The number in hex with prefix: " << std::hex << std::showbase << number << std::endl;

然后就得到我们想要的结果:

The number in octal with prefix: 020

The number in decimal with prefix: 16

The number in hex with prefix: 0x10

固定宽度

在命令行程序中,例如mysql等程序,我们通常可以看到类似表格形式整齐划一的输出形式。这种输出通常是通过固定输出宽度来实现的。那么在c++中,我们如何做到这一点呢?这时候我们需要std::setw的帮助:

std::cout << "The number in octal with fixed width: " << std::setw(8) << std::oct << number << std::endl;
    std::cout << "The number in decimal with fixed width: " << std::setw(8) << std::dec << number << std::endl;
    std::cout << "The number in hex with fixed width: " << std::setw(8) << std::hex << number << std::endl;

然后我们就得到这样的结果:

The number in octal with fixed width:    020

The number in decimal with fixed width:       16

The number in hex with fixed width:     0x10

通常setw默认是使用空白来填充的,但是c++允许我们使用其他的自定的符号:

std::cout << "The number in octal with fixed width and custom fill: " << std::setfill('-') << std::setw(8) << std::oct << number << std::endl;
    std::cout << "The number in decimal with fixed width and custom fill: " << std::setfill('-') << std::setw(8) << std::dec << number << std::endl;
    std::cout << "The number in hex with fixed width and custom fill: " << std::setfill('-') << std::setw(8) << std::hex << number << std::endl;

这里我们使用“-”来填充,然后我们就得到:

The number in octal with fixed width and custom fill: -----020

The number in decimal with fixed width and custom fill: ------16

The number in hex with fixed width and custom fill: ----0x10

当然很多时候我们不希望数值在右边,希望它们在左对齐,那么我们也可以这么做:

std::cout << "The number in octal left with fixed width and custom fill: " << std::setfill('-') << std::left << std::setw(8) << std::oct << number << std::endl;
    std::cout << "The number in decimal left with fixed width and custom fill: " << std::setfill('-') << std::left << std::setw(8) << std::dec << number << std::endl;
    std::cout << "The number in hex left with fixed width and custom fill: " << std::setfill('-') << std::left << std::setw(8) << std::hex << number << std::endl;

这样数值就都跑到左边去了:

The number in octal left with fixed width and custom fill: 020-----

The number in decimal left with fixed width and custom fill: 16------

The number in hex left with fixed width and custom fill: 0x10----

大小写

当然有时候我们希望文本以大写的形式统一输出,这个时候就需要借助std::uppercase的力量:

std::cout << "The number in octal left with fixed width and custom fill and uppercase: " << std::setfill('-') << std::left << std::setw(8) << std::uppercase << std::oct << number << std::endl;
    std::cout << "The number in decimal left with fixed width and custom fill and uppercase: " << std::setfill('-') << std::left << std::setw(8) << std::uppercase << std::dec << number << std::endl;
    std::cout << "The number in hex left with fixed width and custom fill and uppercase: " << std::setfill('-') << std::left << std::setw(8) << std::uppercase << std::hex << number << std::endl;

然后我们可以看到十六进制表示中的字母变成了大写:

The number in octal left with fixed width and custom fill and uppercase: 020-----

The number in decimal left with fixed width and custom fill and uppercase: 16------

The number in hex left with fixed width and custom fill and uppercase: 0X10----

布尔值

通常布尔值读取和输出都不会是true和false,而是0或者1替代。但0或者1这样的表示方式并不直接。那么我们又如何确保读取或者输出的是true和false而不是0和1呢?这里就需要std::boolalpha来处理了:

bool boolValue = true;
    std::cout << "The bool output: " << boolValue << std::endl;
    std::cout << "The true/false bool output: " << std::boolalpha << boolValue << std::endl;

这样我们就可以得到:

The bool output: 1

The true/false bool output: true

精度

精度是数值输出一块比较有用的功能,尤其涉及一些金融业务操作的时候,那么c++标准输入输出如何处理数值的进度问题呢?答案是std::setprecision

double floatValue = 3.141592653589793239;
    std::cout << "The float value in the precision 2: " << std::setprecision(3) << floatValue << std::endl;

然后我们就得到了对应的精度结果:

The float value in the precision 2: 3.14

结论

当然还有很多其他的IO辅助操作符,上面是我列出的比较重要的一些。有些操作符即可以在读取也可以在输出的时候使用,比如进制,布尔值,大小写等。而有些只能在输出的时候使用,比如固定宽度和精度等。希望对大家有所帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值