enum _Ios_Fmtflags
{
_S_boolalpha = 1L << 0,
_S_dec = 1L << 1,
_S_fixed = 1L << 2,
_S_hex = 1L << 3,
_S_internal = 1L << 4,
_S_left = 1L << 5,
_S_oct = 1L << 6,
_S_right = 1L << 7,
_S_scientific = 1L << 8,
_S_showbase = 1L << 9,
_S_showpoint = 1L << 10,
_S_showpos = 1L << 11,
_S_skipws = 1L << 12,
_S_unitbuf = 1L << 13,
_S_uppercase = 1L << 14,
_S_adjustfield = _S_left | _S_right | _S_internal,
_S_basefield = _S_dec | _S_oct | _S_hex,
_S_floatfield = _S_scientific | _S_fixed,
_S_ios_fmtflags_end = 1L << 16
};
fmtflags setf(fmtflags flags);
stream.setf(ios::showbase)
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos);
cout.setf(ios::scientific);
cout << 123 << " " << 123.23 << endl;
return 0;
}
./setf
+123 +1.232300e+02
cout.setf(ios::scientific | ios::showpos);
void unsetf(fmtflags flags);
fmtflags flags();
#include <iostream>
using namespace std;
void showflags(ios::fmtflags f);
int main()
{
ios::fmtflags f;
f = cout.flags();
showflags(f);
cout.setf(ios::showpos);
cout.setf(ios::scientific);
f = cout.flags();
showflags(f);
cout.unsetf(ios::scientific);
f = cout.flags();
showflags(f);
return 0;
}
void showflags(ios::fmtflags f)
{
long i;
for (i = 0x4000; i; i = i >> 1)
if (i & f) cout << "1 ";
else cout << "0 ";
cout << "\n";
}
./unsetflags
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
0 0 1 1 0 0 1 0 0 0 0 0 0 1 0
0 0 1 1 0 0 0 0 0 0 0 0 0 1 0
streamsize width(streamsize len);
char fill(char ch);
streamsize precision(streamsize num);
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos);
cout.setf(ios::scientific);
cout << 123 << " " << 123.23 << "\\n";
cout.precision(2); //在小数点后输出两位
cout.width(10); //输出宽度为10个字符
cout << 123 << " ";
cout.width(10); //输出宽度为10个字符
cout << 123.23 << endl;
cout.fill('#');
cout.width(10); //在10个字符宽的输出域中使用'#'字符填充
cout << 123 << " ";
cout.width(10); //输出宽度为10个字符
cout << 123.23 << endl;
return 0;
}
./width
+123 +1.232300e+02
+123 +1.23e+02
######+123 #+1.23e+02
char fill();
streamsize width();
streamsize precision();
操控符函数 | 用途 | 输入/输出 |
boolalpha | 设置boolaphal表置位 | 输入/输出 |
dec | 设置 dec 标志位 | 输入/输出 |
endl | 输出换行符并刷新流的缓冲区 | 输出 |
ends | 输出空字符 | 输出 |
fixed | 设置 fixed 标志位 | 输出 |
flush | 刷新流的缓冲区 | 输出 |
hex | 设置 hex 标志位 | 输入/输出 |
internal | 设置 internal 标志位 | 输出 |
left | 设置 left 标志位 | 输出 |
noboolalpha | 清除 boolaphal 标志位 | 输入/输出 |
noshowbase | 清除 showbase 标志位 | 输出 |
noshowpoint | 清除 showpoint 标志位 | 输出 |
noshowpos | 清除 showpos 标志位 | 输出 |
noskipws | 清除 skipws 标志位 | 输入 |
nounitbuf | 清除 unitbuf 标志位 | 输出 |
nouppercase | 清除 uppercase标志位 | 输出 |
oct | 设置 oct 标志位 | 输入/输出 |
resetiosflags(fmtflags f) | 清除由 f 指定的标志位 | 输入/s输出 |
right | 设置 right 标志位 | 输出 |
scientific | 设置 scientific 标志位 | 输出 |
setbase(int base) | 将数值的基数设为 base | 输入/输出 |
setfill(int ch) | 将填充字符设为 ch | 输出 |
setiosflags(fmtflags f) | 设置由 f 指定的标志位 | 输入/输出 |
setprecision(int p) | 将数值的精度设为 p | 输出 |
setw(int w) | 将输出域的宽度设为w | 输出 |
showbase | 设置 showbase 标志位 | 输出 |
showpos | 设置 showpos 标志位 | 输出 |
skipws | 设置 skipws 标志位 | 输入 |
unitbuf | 设置 unitbuf 标志位 | 输出 |
uppercase | 设置 uppercase 标志位 | 输出 |
ws | 忽略开头的空白 | 输入 |
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout <<setprecision(4) << 100.243 << endl;
cout << setw(20) << "Hello world.";
cout << endl;
return 0;
}
./ioman
100.2
Hello world.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setiosflags(ios::showpos);
cout << setiosflags(ios::scientific);
cout << 123 << " " << 123.23;
return 0;
}
./setios
+123 +1.232300e+02
#include <iostream>
using namespace std;
int main()
{
char s[80];
cin >> ws >> s;
cout << s;
return 0;
}
./ws
world
world
ostream &manip_name(ostream &stream)
{
//自己的代码
return stream;
}
#include <iostream>
#include <iomanip>
using namespace std;
ostream &setup(ostream &stream)
{
stream.setf(ios::left);
stream << setw(10) << setfill('$');
return stream;
}
int main()
{
cout << 10 << " " << setup << 10;
return 0;
}
./sefman
10 10$$$$$$$$
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |