标签:oct 修改 打印 包含 一个 oat code 运算符 sqrt
标准库定义了操纵符来修改流的状态,endl 就是一个操纵符,它输出一个换行符并刷新缓冲区。
cout << "default bool values: " << true << " " << false << endl;
cout << "alpha bool values: " << boolalpha << true << " " << false << endl;
cout << noboolalpha;
cout << true << endl;
cout << "default:" << 20 << " " << 1024 << endl;
cout << "octal:" <<oct<< 20 << " " << 1024 << endl;
cout << "hex:" << hex<<20 << " " << 1024 << endl;
cout << "decimal:" << dec << 20 << " " << 1024 << endl;
cout << showbase;
cout << "default:" << 20 << " " << 1024 << endl;
cout << "in oct:" << oct<<20 << " " << 1024 << endl;
cout << "in hex:" << hex<<20 << " " << 1024 << endl;
cout << "in decimal:" << dec<<20 << " " << 1024 << endl;
cout << noshowbase;
浮点数的输出格式控制分为:
默认情况下:
cout << "Precision: " << cout.precision() << endl; //@ 查看当前的打印精度
cout << "Values: " << sqrt(2.0) << endl;
cout.precision(12); //@ 设置打印精度
cout << "Precision:" << cout.precision() << endl;
cout << "Values: " << sqrt(2.0) << endl;
cout << setprecision(3);
cout << "Values: " << sqrt(2.0) << endl;
cout << showpoint;
cout << 3.0 << endl;
cout << noshowpoint;
cout << 3.0 << endl;
cout << "default format:" << 100 * sqrt(2.0) << endl;
cout <<"scientific format:"<< scientific << 100 * sqrt(2.0) << endl;
cout <<"fixed decimal format:" << fixed <<100 * sqrt(2.0) << endl;
cout << "hex decimal format:" << hexfloat << 100 * sqrt(2.0) << endl;
cout <<"use defaults:"<<defaultfloat<< 100 * sqrt(2.0) << endl;
操纵符 | 说明 |
---|---|
uppercase | 十六进制 0X,科学计数法 E |
nouppercase | 十六进制 0x,科学计数法 e |
showpos | 对非负数显示 + |
noshowpos | 对非负数不显示 + |
left | 在值的右侧添加填充字符 |
right | 在值的左侧添加填充字符 |
internal | 在符号和值之间添加填充字符 |
unitbuf | 每次输出操作后都刷新缓冲区 |
nounitbuf | 恢复正常的缓冲区刷新方式 |
skipws | 输入运算符跳过空白符 |
noskipws | 输入运算符不跳过空白符 |
flush | 刷新 ostream 缓冲区 |
ends | 插入空字符,然后刷新 ostream 缓冲区 |
endl | 插入换行,然后刷新 ostream 缓冲区 |
操纵符 | 说明 |
---|---|
setfill(ch) | 用 ch 填充空白 |
setprecision(n) | 将浮点精度设置为n |
set(w) | 读或写值的宽度为w个字符 |
setbase(b) | 将整数输出为b进制 |
int i = -16;
double d = 3.14159;
//@ 默认右对齐
cout << "i: " << setw(12) << i << endl;
cout << "d: " << setw(12) << d << endl;
cout << left; //@ 设置左对齐
cout << "i: " << setw(12) << i << endl;
cout << "d: " << setw(12) << d << endl;
//@ 恢复右对齐
cout << right;
cout << "i: " << setw(12) << i << endl;
cout << "d: " << setw(12) << d << endl;
//@ 补白第一列,但补在域的内部
cout << internal;
cout << "i: " << setw(12) << i << endl;
cout << "d: " << setw(12) << d << endl;
cout << setfill('#');
cout << "i: " << setw(12) << i << endl;
cout << "d: " << setw(12) << d << endl;
setfill(' ');
默认情况下,输入运算符会忽略空白符(空格符,制表符,换行符,换纸符,回车符)。
操纵符 noskipws 会令输入运算符读取空白符,而不是跳过,恢复默认行为可以使用 skipws。
char ch;
while (cin >> ch) //@ 输入 ab c
cout << ch; //@ 输出 abc
cin >> noskipws;
while (cin >> ch) //@ ab c
cout << ch; //@ ab c
cin >> skipws;
标签:oct 修改 打印 包含 一个 oat code 运算符 sqrt
原文地址:https://www.cnblogs.com/xiaojianliu/p/12398238.html