(2)清除状态标志 10 清除某一状态标志,即是将某一状态标志位置”0”,可使用 unsent0函数,使用时的调用格式与set(相同。 (3)取状态标志 取一个状态标志,可使用 flags0函数。fags0函数有不带参数 与带参数两种形式: ling ios flags(; 用于返回当前的状态标志字; ling ios: flags( log flag);返回当前的状态标志字后,再 者将状态标志字设置成fag(参数) flags( log flag)函数与set( Ciog flags)函数的差别在于:sef0函数 是在原有的基础上追加设定的,而fags函数是用新设定覆盖 以前的状态标志字。 例72几个成员函数的使用方法 include <iostream. h> void showflags(ongf)∥输出状态标志字函数 I long i; for(i=0x8000i;ii>1)∥用右移方法使ⅰ中的值为”1的位不断右 移 if(i&f)cout<”1”; ∥.断中的某一位是否为”1 else cout<<”0” cout<<end
(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 i long f; cout setf(ios: unitbuf ios: skipws); f= cout. flags(;∥取当前状态标志字 showbags(f) ∥示状态标志字 cout. setf(ios: showpos|ios: scientific);追加状态标志位 f=cout. flags showbags(o); cout, unset(ios: scientific);∥从状态标志字中去掉 sciengific f=cut. flags; showbags(f); f= cout. flags(ios:loct;:程序运行结果为; showflags(); 0010000000000001(1) f=cout. flags 001011000000001(2) showbags(t) 0010010000000001(3) 000000000100000(4) 000000000100000(5)
接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)填充字符 填充字符的作用是:当输出值不满域宽时用填充字符来填充, 缺省情况下填充字符为空格,所以在使用填充字符函数时,必须 与wdh(函数相配合,否则就没有意义,在ios类中用数据成员 ⅹ_fi来存放填充字符。填充字符的成员函数有两个,其一般形 式为: char ios:fil(; 返回当的填充字符 char ios:fil( 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 13 include <iostream h> void maino i cout<<x width<<cout width(<<endl; cout<<”Xfil=”<< cout,fil<<endl cout<<x_precision='<<cout precision(<<endl cout<123<<<12345678<<endl; cout<<” cout<<****x width=10, x_fill,x_precision=4****In", cout width (10) cout. precision( 4 cout<<123<<“<123.45678<<“<<234.567<<endl; cout≤< x width=”<< cout, width≤≤endl; cout<s”Xfil=”<< cout, fil<endl; cout<<x_precision<<cout. precision(<<endl; cout<<”- cout<< ****x_ width=10, x_ fil&, x_precidion=4****In
例 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 14 cout fill('r') 程序运行结果如下: x width=0 x fillE x precision 123123.45678 x>>x width=10,x fill x precision=4 *xxx 123123.4568234.567 x width=0 Ix fill= x_ precision=4 ■■■ ****x width=10, x_ fill=&, x_precision=4**** 8888888123123.4568 12388888818123.4568 x width=0 x fill=& 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