C++常用的I/O流控制符2009-05-12 15:31
控制符
含义
dec
10进制输出数据
hex
16进制输出数据
oct
8进制输出数据
setw(n)
设置每个数占用宽度
setprecision(n)
设置小数位数(含小数点)
setioflags(ios::uppercase)
16进制大写输出数据
setioflags(ios::lowercase)
16进制小写输出数据
setioflags(ios::left)
左对齐
setioflags(ios::right)
右对齐
setioflags(ios::showpos)
设置正、负号的显示
setfill(c)
设置填充字符为c
endl
换行符
I/O流控制符。
参考源代码:
/* 例2-3-1,2-3-1.cpp */
#include
#include
#include
using namespace std ;
void main()
{
double amount = 22.0/7 ;
int number = 1001 ;
cout << amount << endl ;
cout << setprecision(0) << amount << endl /
<< setprecision(1) << amount << endl /
<< setprecision(2) << amount << endl /
<< setprecision(3) << amount << endl /
<< setprecision(4) << amount << endl ;
cout << setiosflags(ios :: fixed);
cout << setprecision(8) << amount << endl ;
cout << "Decimals:" << dec << number << endl /
<< "Hexadecimal:" << hex << number << endl /
<< "Octal:" << oct << number << endl ;
cout << setiosflags(ios :: scientific) << amount << endl ;
cout << setprecision(6);
system("pause");
}
运行结果:
3.14286
3.14286
3
3.1
3.14
3.143
3.14285714
Decimals:1001
Hexadecimal:3e9
Octal:1751
3.1428571
c++ i/o控制符
最新推荐文章于 2025-08-06 16:37:51 发布