iomanip部分函数功能,流操作的控制

小数输出例子: 

 #include<iostream>
#include<iomanip>
using namespace std;
void main()
{
    double a = 123.456789012345;   
    cout << a << endl;
    cout << setprecision(9) << a << endl;
    cout << setprecision(6);
    cout << setiosflags(ios::fixed);//
    cout << setiosflags(ios::fixed) << setprecision(8) << a << endl;  
    cout << setiosflags(ios::scientific) << a << endl;
    cout << setiosflags(ios::scientific) << setprecision(4) << a << endl;
}

cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);

setiosflags 是包含在命名空间iomanip 中的C++ 操作符,该操作符的作用是执行由有参数指定

区域内的动作;

iso::fixed

操作符setiosflags 的参数之一,该参数指定的动作是以带小数点的形式表示浮点

数,并且在允许的精度范围内尽可能的把数字移向小数点右侧;

iso::right:

setiosflags 的参数,该参数的指定作用是在指定区域内右对齐输出;

setprecision :(相当于cout.precision

也是包含在命名空间iomanip 中的C++ 操作符,该操作符的作用是设定浮点数;

setprecision(2) 的意思就是小数点输出的精度,即是小数点右面的数字的个数为2。

cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);

合在一起的意思就是,输出一个右对齐的小数点后两位的浮点数。

使用setprecision(n)可控制输出流显示浮点数的数字个数。C++默认的流输出数值有效位是6。

如果setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。

整数输出例子:

#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
	int b = 123456;
	cout << b << endl;
	cout << hex << b << endl;
	cout << setiosflags(ios::uppercase) << b << endl;
	cout << setw(10) << b << ',' << b << endl;
	cout << setfill('*') << setw(10) << b << endl;
	cout << setiosflags(ios::showpos) << b << endl;
}

setiosflags(ios::fixed)是用表示实数

setioflags(ios::fixed) 固定的浮点显示
setioflags(ios::scientific) 指数表示
setiosflags(ios::left) 左对齐
setiosflags(ios::right) 右对齐
setiosflags(ios::skipws) 忽略前导空白
setiosflags(ios::uppercase) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
setiosflags(ios::showpoint) 强制显示小数点和无效0
setiosflags(ios::showpos) 强制显示符号

setw(n) 设域宽为n个字符
setfill(int c)设置填充字符的ASCII码为c的字符【setfill('*')也可以】


还有部分操作符不需要调用成员函数:

dec:转换为按十进制输出整数,是默认的输出格式。

oct:转换为按八进制输出整数。

hex:转换为按十六进制输出整数。

ws:从输出流中读取空白字符。

endl:输出换行符\n并刷新流。刷新流是指把流缓冲区的内容立即写入到对应的物理设备上。

ends:输出一个空字符\0

flush:只刷新一个输出流


参考:https://blog.csdn.net/qq_44537267/article/details/103269208

参考:C++中setiosflags()函数的用法_梦里通天塔的编程小屋-CSDN博客_setiosflags函数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值