(2)清除状态标志 10 清除某一状态标志,即是将某一状态标志位置”0”,可使用 unset(函数,使用时的调用格式与setf相同。 (3)取状态标志 取一个状态标志,可使用 flags0函数。 flags(函数有不带参数 与带参数两种形式 ling ios flags; 用于返回当前的状态标志字; ling ios: flags( log flag);返回当前的状态标志字后,再 者将状态标志字设置成fag(参数)。 flags( log flag函数与set( iog flags)函数的差别在于:set0函数 是在原有的基础上追加设定的,而 flags0函数是用新设定覆盖 以前的状态标志字。 例72几个成员函数的使用方法 include <iostream h> void showflags(ongf)∥输出状态标志字函数 I long i for(=0x8000;i=i>1川∥用右移方法使ⅰ中的值为”1”的位不断右 移 if(8& f couts<”1” ∥)断f中的某一位是否为”1 e| se cout<<”0” t<<endl
(2)清除状态标志 清除某一状态标志,即是将某一状态标志位置”0”,可使用 unseft()函数,使用时的调用格式与setf()相同。 (3)取状态标志 取一个状态标志,可使用 flags()函数。flags()函数有不带参数 与带 参数两 种形式: ling ios ::flags(); 用于返回当前的状态 标志字; ling ios ::flags (liog flag); 返回当前的状态 标志字后,再 者将状态标志字设置成flag(参数)。 flags(liog flag)函数与setf(liog flags)函数的差别在于:setf()函数 是在原有的基础上追加设定的,而flags()函数是用 新设定覆盖 以前的状态标志字。 例 7.2 几个成员函数的使用方法 #include <iostream.h> void showflags(long f) //输出状态标志字函数 { long i; for(i=0x8000;i;i=i>>1)//用右移方法使i中的值为”1”的位不断右 移 if (i&f) cout<<”1”; //判断f中的某一位是否为”1” else cout <<”0”; cout<<endl; } 10
接1例72 void maino long f cout. setf(ios: unitbufios: skipws); f=cout. flags ∥取当前状态标志字 showflags(f) ∥显示状态标志字 cout. setf(ios: showpos|ios: cientific));追加状态标志位 f=cout. flags showflags(f) cout, unseti(ios: scientific);∥从状态标志字中去掉 scientific f=cut. flags: showflags f); f= cout. flags(ios:oct);程序运行结果为; showflags() 00100000000000) f=cout. flags 001011000000001(2) showflags(f) 001001000000001(3) 000000000100000(4) 00000000001000005
接1 例 7.2 void main() { long f; cout.setf(ios::unitbuf|ios::skipws); f=cout.flags(); //取当前状态标志字 showflags(f); //显示状态标志字 cout.setf(ios::showpos|ios::scientific); //追加状态 标志位 f=cout.flags(); showflags(f); cout.unsetf(ios::scientific); //从状态标志字中去掉 sciengific f=cut.flags(); showflags(f); f=cout.flags(ios::oct); //重新设置状态标志字 showflags(f); //显示设置前的状态标志字 f=cout.flags(); //取设置 后的状态标志字 showflags(f); //显示设置后的状态标志字 } 11 程序运行结果为; 0010000000000001 (1) 0010110000000001 (2) 0010010000000001 (3) 0000000000100000 (4) 0000000000100000 (5)
(4)设置域宽 12 域宽主要用来控制输出,在ios类中域宽存放在数据成员int x width中,设置域宽的成员函数有两个,其一般格式为: int ios width( 返回当的域宽值 int ios width(int w) 设置域宽,并返回原来的域宽 (5)设置输出的精度 精度用来控制浮点数的输出显示精度,在ios类中用数据成员int x_ precision来存放精度,设置精度的成员函数的一般格式为: int ios: precision(intp);重新设置浮点数所需小数的位数并返 回设置前的位数 (6)填充字符 填充字符的作用是:当输出值不满域宽时用填充字符来填充, 缺省情况下填充字符为空格,所以在使用填充字符函数时,必须 与wdh0函数相配合,否则就没有意义,在ios类中用数据成员 ⅹ_fi来存放填充字符。填充字符的成员函数有两个,其一般形 式为: char ios:f() 返回当的填充字符 char ios: fill(char ch);用ch重新设置填充字符,并返 回设置前的填充字符
(4)设置域宽 域宽主要用来控制输出,在ios 类中域 宽存放在数据成员int x_width中,设置域宽的成员函数有两个,其一般格式为: int ios ::width(); 返回当的域宽值 int ios ::width(int w); 设置域宽,并返回原来的域 宽 (5)设置输出的精度 精度用来控制浮点数的输出显示精度,在ios类中用数据成员int x_precision来存放精度,设置精度的成员函数的一般格式为: int ios::precision(int p); 重新设置浮点数所需小数的位数,并返 回设置前的位数 (6)填充字符 填充字符的作用是:当输出值不满域宽时用填充字符来填充, 缺省情况下填充字符为空格,所以在使用填充字符函数时,必须 与width()函数相配合,否则就没有意义,在ios类中用数据成员 x_fill来存放填充字符。填充字符的成员函数有两个,其一般形 式为: char ios ::fill(); 返回当的填充字符 char ios ::fill(char ch); 用ch重新设置填充字符,并返 回设置前的填充字符。 12
例73 include <iostream. h> void maino i cout<<x width= <<cout width(<<endl cout<< x fills<<cout fillo<<endl cout<<x_precision=><<cout precision(<<end; cout<<123<<“<12345678<<end; cout<< cout<<***x width=10, x fill x_precision=4****In" cout width(10) cout. precision(4) cout<<123<<”“<<123.45678<<”“<<234.567<<end; cout<s” x width=”< <cout width(<≤endl; cout<< x fills<<cout fillo<<endl cout<”X_ precision=”<< cout. precision(≤end; cout<<”- cout<”* x width=10,Xfi=8, x_precision=4*n”;
例 7.3 #include <iostream.h> void main() { cout<<”x_width=”<<cout.width()<<endl; cout<<”x_fill=”<<cout.fill()<<endl; cout<<”x_precision=”<<cout.precision()<<endl; cout<<123<<” “<<123.45678<<endl; cout<<”---------------------------------------\n”; cout<<”****x_width=10,x_fill= ,x_precision=4****\n”; cout.width(10); cout.precision(4); cout<<123 <<” “<<123.45678<<” “<<234.567<<endl; cout <<”x_width=”<<cout.width()<<endl; cout<<”x_fill=”<<cout.fill()<<endl; cout<<”x_precision=”<<cout.precision()<<endl; cout<<”---------------------------------------\n”; cout<<”****x_width=10,x_fill=&,x_precidion=4****\n”; 13
接1例73 4 cout fill(&'). 程序运行结果如下: x width=0 x fillE x precision=O 123123.45678 ****x width=10,x fill=, x_precision=4**** 1231234568234.567 x width=0 3x fille x_ precision=4 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ x width=10, x fill=& x_ precision=4 8888888123123.4568 1238888888123.4568 x width=0 x fillE=& x precIsion=4
接1 例 7.3 cout.fill(‘&’); cout.width(10); cout<<123<<” “<<123.45678<<endl; cout.setf(ios::left); cout.width(10); cout<<123<<” “<<123.45678<<endl; cout<<”x_width=”<<cout.width()<<endl; cout<<”x_fill=”<<cout.fill()<<endl; cout<<”x_precision=“<<cout.precision()<<endl; } 14 程序运行结果如下: x_width=0 x_fill= x_precision=0 123 123.45678 ------------------------------------- ****x_width=10,x_fill= ,x_preciosion=4**** 123 123.4568 234.567 x_width=0 x_fill= x_precision=4 ------------------------------------- ****x_width=10,x_fill=&,x_precision=4**** &&&&&&&123 123.4568 123&&&&&&& 123.4568 x_width=0 x_fill=& x_precision=4