#include<iostream> using namespace std; int main() { ios_base::fmtflags old_val = cout.flags( ios_base::left | ios_base::hex |ios_base::uppercase | ios_base::showbase ); for( int i = 100; i < 150; i ++ ) cout << i << endl; cout<<endl<<endl; cout.unsetf ( ios_base::left | ios_base::hex |ios_base::uppercase ); cout.setf ( old_val ); for( int i = 100; i < 150; i ++ ) cout << i << endl; cout<<endl<<endl; cout.unsetf( old_val ); cout.setf ( ios_base::hex | ios_base::uppercase ); cout << " hex " << 170 << endl; cout.unsetf( ios_base::hex | ios_base::uppercase ); cout.setf ( ios_base::oct ); cout << " oct " << 170 << endl; cout.unsetf( ios_base::oct ); cout.setf ( ios_base::dec ); cout << " dec " << 170 << endl; return 0; }
#include<iostream> using namespace std; int main() { cout.width ( 6 ); cout << -100 << endl; cout.setf( ios_base::left ,ios_base::adjustfield); cout.width ( 6 ); cout << -100 << endl; cout.unsetf( ios_base::left ); cout.setf( ios_base::right ,ios_base::adjustfield); cout.width ( 6 ); cout << -100 << endl; cout.unsetf( ios_base::right ); return 0; }
在本例中,我们调用成员函数 width() 控制输出的宽度为 6 个字符的位置,之后分别设置左对齐和右对齐,然后输出 -100。从上面程序中我们可以看出默认是采用右对齐的方式输出的。