流操纵算子

1、流操纵算子可以用来干什么?

  • 设置整数的进制(八进制、十六进制等)
  • 设置浮点数的精度(setprecision)
  • 设置域宽(setw)
  • 设置对齐格式等(左对齐、右对齐等)
  • 其它功能及自定义流操纵算子

注意: 使用流操纵算子需要 #include <iomanip>

2、控制整数进制的流操纵算子

  • hex 16进制
  • dec 10进制
  • otc 8进制

使用举例:

int main(){
	int n = 10;
	cout << n << endl;
	cout << hex << n << “
”			//16进制
	<< dec << n << “
”				//10进制
	<< oct << n << endl;				//8进制
}
/*
输出结果:
10
a
10
12
*/

3、控制浮点数精度的方式

(1)setprecision 流操纵算子方式,调用方式:
	cout << setprecision(5); // 可以连续输出
(2)precision 调用cout的成员函数方式,调用方式:
	cout.precision(5);
(3)浮点数定点与非定点的区别
  • 指定输出浮点数的有效位数(非定点方式输出时)
  • 指定输出浮点数的小数点后的有效位数(定点方式输出时)
  • 定点方式:小数点必须出现在个位数后面
(4)浮点数定点与非定点格式控制:
  • setiosflags(ios::fixed):设置小数点格式的函数,不属于流操纵算子。以小数点位置固定的方式输出
  • resetiosflags(ios::fixed):设置小数点格式的函数,不属于流操纵算子。取消以小数点位置固定的方式输出
(5)使用举例:
  • 例1:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double x = 1234567.89,y = 12.34567;
	int n = 1234567;
	int m = 12;
	cout << setprecision(6) << x << endl
	<< y << endl << n << endl << m;
}
/*
输出:
1.23457e+006
12.3457
1234567
12
*/
  • 例2:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double x = 1234567.89;
	cout << setiosflags(ios::fixed) <<
	setprecision(6) << x << endl <<
	resetiosflags(ios::fixed) << x ;
}
/*
输出:
1234567.890000
1.23457e+006
*/

3、控制域宽的方式

(1)流操纵算子方式
  • setw() 设置域宽流操纵算子。
(2)成员函数方式
  • cin.width()、cout.width() 设置域宽的成员函数。
(3)两种方式区别

两者功能相同,一个是成员函数,另一个是流操作算子,调用方式不同:

	cin >> setw(4); 
	cin.width(5);//1、2等价
	cout << setw(4); 
	cout.width(5);//3、4等价

注意:宽度设置有效性是一次性的,在每次读入和输出之前都要设置宽度。

(4)应用举栗
void main(){
	int w = 4;
	char string[10];
	cin.width(5);//指定输入的宽度为5,即只读5个字符。其中包含结尾的'',因此实际读入的字符只有4个。
	while(cin >> string){
		cout.width(w++); //设置输出的字符宽度,不足4个字符的在左边补空格
		cout << string << endl;
		cin.width(5);
	}
}
/*
输入:
1234567890
输出:
1234
5678
90
*/

4、综合例子

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
	int n = 141;
	//1) 分别以十六进制、十进制、八进制先后输出 n
	cout << "1) " << hex << n << " " << dec << n << " " << oct << n << endl;
	double x = 1234567.89,y = 12.34567;
	//2) 保留5位有效数字
	cout << "2) " << setprecision(5) << x << " " << y << " " << endl;
	//3) 保留小数点后面5位
	cout << "3) " << fixed << setprecision(5) << x << " " << y << endl ;
	//4) 科学计数法输出,且保留小数点后面5位
	cout << "4) " << scientific << setprecision(5) <<x << " " << y << endl ;
	//5) 非负数要显示正号,输出宽度为12字符,宽度不足则用'*'填补
	cout << "5) " << showpos << fixed << setw(12) << setfill('*') << 12.1
	<< endl;
	//6) 非负数不显示正号,输出宽度为12字符,宽度不足则右边用填充字符填充
	cout << "6) " << noshowpos << setw(12) << left << 12.1 << endl;
	//7) 输出宽度为12字符,宽度不足则左边用填充字符填充
	cout << "7) " << setw(12) << right << 12.1 << endl;
	//8) 宽度不足时,负号和数值分列左右,中间用填充字符填充
	cout << "8) " << setw(12) << internal << -12.1 << endl;
	cout << "9) " << 12.1 << endl;
	return 0;
}
/*
1) 8d 141 215
2) 1.2346e+006 12.346
3) 1234567.89000 12.34567
4) 1.23457e+006 1.23457e+001
5) ***+12.10000
6) 12.10000****
7) ****12.10000
8) -***12.10000
9) 12.10000
*/

例子中用到的流操纵算子:

  • fixed 设置定点格式
  • scientific 科学计数法格式
  • showpos非负数不显示负号
  • noshowpos非负数显示符号
  • setfill('*') 空白补星
  • left 左对齐
  • right 右对齐
  • internal 中间对齐

5、用户自定义的流操纵算子

ostream &tab(ostream &output){
	return output << '	';
}
cout << “aa” << tab << “bb” << endl;
//输出:输出:aa		bb  

为什么tab真的能输出tab(屏幕显示形式为多个空格)?
因为 iostream 里对 << 进行了重载(成员函数)

ostream & operator<<( ostream & ( * p ) ( ostream & ) ) ;

该函数内部会调用p所指向的函数,且以 *this 作为参数hexdecoct 都是函数。

原文地址:https://www.cnblogs.com/lasnitch/p/12764238.html