奔流成员函数进行输入输出格式控制
涌流成员函数进行输入输出格式控制
1。设置状态标志位的流成员函数 setf() 一般格式: long ios::setf(long flags); 调用格式: 流对象.setf(ios::状态标志) 状态的作用如下: ios::skipws //跳过输入中的空白符,用于输出 ios::left //输出数据在本域宽范围内左对齐,用于输出 ios::right //输出数据在本域宽范围内右对齐,用于输出 ios::internal //数据的符号位左对齐,数据本身右对齐,符号与数据之间为填充符,用于输出 ios::dec //设置数据的基数为10进制,用于输入/输出 ios::oct //设置数据的基数为8进制,用于输入/输出 ios::hex //设置数据的基数为16进制,用于输入/输出 ios::showbase //输出数据使显示基数符号(八进制O开头,十六进制Ox开头)用于输入输出 ios::showpoint //浮点数输出是带有小数点,用于输出 ios::uppercase //在以科学表示法格式E和十六进制输出字母时用大写表示,用于输出 ios::showpos //正整数前显示“+”,用于输出 ios::scientific //用科学表示法格式(指数)显示浮点数,用于输出 ios::fixed //固定小数位数显示浮点数用于输出 ios::unitbuf //完成输出操作后立即刷新所有流,用于输出 ios::stdio //完成输出操作后刷新stdout和stderr,用于输出 如下例:#include<iostream> using namespace std; int main() { cout<<"--------------1--------------\n"; cout.width(10); cout<<123<<endl; cout<<"-----------2-------------\n"; cout<<123<<endl; cout<<"-----------3-------------\n"; cout.fill('%'); cout.width(10); cout<<123<<endl; cout<<"-----------4-------------\n"; cout.setf(ios::left); cout<<123<<endl; cout<<"--------------5----------------\n"; cout.precision(4); cout<<123.456789<<endl; cout<<"--------------6-----------------\n"; cout.setf(ios::fixed); cout<<123.456789<<endl; cout<<"---------------7---------------\n"; cout.width(15); cout.unsetf(ios::fixed); cout.setf(ios::scientific); cout<<123.45678<<endl; cout<<"--------------8------------------\n"; int a=21; cout.setf(ios::showbase); cout.unsetf(ios::dec); cout.setf(ios::hex); cout<<a<<endl; system("pause"); return 0; }
2.清除状态标志的流成员函数 unsetf()
一般格式: long ios::unsetf(long flags)
调用格式:
流对象.unsetf(ios::状态标志)
3.设置域宽的流成员函数 width()
格式:
int ios::width(int n)
4.设置实数精度的流成员函数 precision()
格式 int ios::precision(int n)
5.填充字符的流成员函数 fill()
char ios::fill(char ch);