C++之cout

一、流算子

以往要输出不同进制的数值,需要先unsetf取消当前进制,setf设置当前进制,下面给出了代码例子:

#include <iostream>

using namespace std;

int main()
{
    int a = 0x12345678;
    cout.unsetf(ios::dec);
    cout.setf(ios::hex);
    cout << "十六机制: " << a << endl;
    cout.unsetf(ios::hex);
    cout.setf(ios::oct);
    cout << "八机制: " << a << endl;
    cout.unsetf(ios::oct);
    cout.setf(ios::dec);
    cout << "十机制: " << a << endl;
    system("pause");
    return 0;
}

输出结果:

这里写图片描述

可见这种方式一是麻烦,二很容易犯错,如果忘记取消当前进制,即使设置了,也不起作用。

1、进制转换

所以就引入的流算子,流算子需要添加头文件<iomanip>,把上面的代码用流算子改一下:

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    int a = 0x12345678;
    cout << "十六进制:" << hex << a << endl;
    cout << "八进制:" << oct << a << endl;
    cout << "十进制:" << dec << a << endl;
    system("pause");
    return 0;
}

输出结果:

这里写图片描述

代码大大简化了,书写也方便了有木有。

2、设置域宽

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    int a = 123;
    cout << setw(1) << a << "---" << endl;
    cout << setw(3) << a << "---" << endl;
    cout << setw(5) << a << "---" << endl;
    system("pause");
    return 0;
}

输出结果:

这里写图片描述

可以看到若设置的域宽小于等于原本数据应该占用的域宽,就会把原数据原样输出,如果设置的域宽大于原本数据应该占用的域宽,则会在左边填充空格。

3、设置填充字符

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    int a = 123;
    cout << setfill('*') << setw(5) << a << "---" << endl;
    system("pause");
    return 0;
}

输出结果

这里写图片描述

4、设置对齐方式

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    int a = 123;
    cout << setiosflags(ios::left) << setfill('*') << setw(5) << a << "---" << endl;
    cout << setiosflags(ios::right) << setfill('*') << setw(5) << a << "---" << endl;
    system("pause");
    return 0;
}

输出结果:

这里写图片描述

跟上面结果进行比对可以发现,默认是右对齐。

5、强制显示

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    double a = 10 / 5;
    double b = 26.0 / 7;
    cout << a << endl;
    cout << setiosflags(ios::showpoint) << a << endl;
    cout << b << endl;
    cout << setiosflags(ios::showpos) << b << endl;
    system("pause");
    return 0;
}

输出结果:

这里写图片描述

showpoint强制显示小数位,showpos强制显示正负。

6、设置精度

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    double a = 123.456789;
    cout << setprecision(1) << a << endl;
    cout << setprecision(4) << a << endl;
    cout << setprecision(8) << a << endl;
    cout << setprecision(12) << a << endl;
    system("pause");;
    return 0;
}

输出结果:

这里写图片描述

当设置的精度小于原本的精度时,系统会四舍五入截断数据,当设置的精度大于原本的精度时,只会输出原本的精度。

7、设置浮点数的输出是以科学计数法还是定点数

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    double a = 123.45678913;
    cout << a << endl;
    cout << setiosflags(ios::scientific) << a << endl;
    cout << resetiosflags(ios::scientific);
    cout << setiosflags(ios::fixed) << a << endl;
    cout << setprecision(2) << setiosflags(ios::fixed) << a << endl;
    cout << setprecision(12) << setiosflags(ios::fixed) << a << endl;
    system("pause");;
    return 0;
}

输出结果:

这里写图片描述

scientific设置浮点数以科学计数法形式输出,fixed使此时的精度域表示小数位数,原本的精度域是包括整数与小数一起的。setprecision(2) 与setiosflags(ios::fixed)一起使用时表示设置的小数精度。

注意:设置fixed时,必须先把scientific取消掉。

8、输出十六进制时控制字母的大小写

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    int a = 0xabcd;
    cout << hex << a << endl;
    cout << hex << setiosflags(ios::uppercase) << a << endl;
    cout << hex << resetiosflags(ios::uppercase) << a << endl;
    system("pause");
    return 0;
}

输出结果:

这里写图片描述

对于十六进制系统默认是输出小写,通过uppercase设置输出大写,取消设置则回到小写。

二、成员函数

ostream put(char)

功能: 输出一个字符

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    char buf[] = "hello world";
    for (int i = sizeof(buf) / sizeof(char) - 2; i >= 0; i--)
        cout.put(*(buf + i));
    cout.put('\n');
    system("pause");;
    return 0;
}

输出结果:

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值