#include<iostream>usingnamespace std;intmain(){int n =11;
cout.flags(ios::dec);
cout << n << endl;
cout.flags(ios::hex);
cout << n << endl;
cout.flags(ios::oct);
cout << n << endl;
cout.flags(ios::showbase|ios::dec);
cout << n << endl;
cout.flags(ios::showbase|ios::oct);
cout << n << endl;
cout.flags(ios::showbase|ios::hex);
cout << n << endl;
cout.flags(ios::showbase|ios::uppercase|ios::dec);
cout << n << endl;
cout.flags(ios::showbase|ios::uppercase|ios::oct);
cout << n << endl;
cout.flags(ios::showbase|ios::uppercase|ios::hex);
cout << n << endl;}
操作子方式
#include<iostream>usingnamespace std;intmain(){int n =11;
cout << dec << n << endl;
cout << hex << n << endl;
cout << oct << n << endl;
cout << showbase << dec << n << endl;
cout << oct << n << endl;
cout << hex << n << endl;
cout << uppercase << dec << n << endl;
cout << oct << n << endl;
cout << hex << n << endl;}
#include<iostream>// cout, std::fixed, std::scientificusingnamespace std;intmain(){double a =3.1415926534;double b =2006.0;double c =1.0e-10;
cout.precision(5);
cout <<"default:\n";
cout << a << endl << b << endl << c << endl;
cout <<"fixed:\n";
cout.flags(ios::fixed);
cout << a << endl << b << endl << c << endl;
cout <<"scientific:\n";
cout.flags(ios::scientific);
cout << a << endl << b << endl << c << endl;return0;}
操作子方式
#include<iostream>// std::cout, std::fixed, std::scientific#include<iomanip>usingnamespace std;intmain(){double a =3.1415926534;double b =2006.0;double c =1.0e-10;
cout <<setprecision(5)<<"default:\n"<< a << endl << b << endl << c << endl;
cout <<"fixed:\n"<< fixed
<< a << endl << b << endl << c << endl;
cout <<"scientific:\n"<< scientific
<< a << endl << b << endl << c << endl;return0;}
混合方式
#include<iostream>// std::cout, std::fixed, std::scientificintmain(){double a =3.1415926534;double b =2006.0;double c =1.0e-10;
std::cout.precision(5);
std::cout <<"default:\n";
std::cout << a <<'\n'<< b <<'\n'<< c <<'\n\n'
std::cout <<"fixed:\n"<< std::fixed;
std::cout << a <<'\n'<< b <<'\n'<< c <<'\n\n'
std::cout <<"scientific:\n"<< std::scientific;
std::cout << a <<'\n'<< b <<'\n'<< c <<'\n\n';return0;}
5.6.4 布尔类型输出格式
flag
manipulator
作用
默认
ios::boolalpha
boolalpha
把bool值以字符串true/false输出
否
5.6.5 其他
flag
manipulator
作用
默认
ios::showpos
showpos
输出十进制0或者正数时,带+号
否
成员函数方式
#include<iostream>// std::cout, std::showpos, std::noshowposusingnamespace std;intmain(){int p =1;int z =0;int n =-1;
cout.setf(ios::showpos); cout << p <<'\t'<< z <<'\t'<< n << endl;
cout.unsetf(ios::showpos); cout << p <<'\t'<< z <<'\t'<< n << endl;return0;}
操作子方式
#include<iostream>// std::cout, std::showpos, std::noshowposusingnamespace std;intmain(){int p =1;int z =0;int n =-1;
cout << showpos << p <<'\t'<< z <<'\t'<< n << endl;
cout << noshowpos << p <<'\t'<< z <<'\t'<< n << endl;return0;}