本博客根据《c++ Primer Plus 中文版第六版 》编写
对于cout,有时候我们知之甚少。
一. 修改显示时使用的计数系统
就是关于进制之间的转化。我们知道,C语言中 用%x %o 等等转化,而c++有特定的函数。
格式是
cout<<hex; 转化为16进制
cout<<oct; 转化为8进制
cout<<dec; 转化为10进制
详情参见 The first test
二. 调整字段宽度
学会这个函数,我们调整界面格式就不必敲空格了。
作用是将长度不同的数字放到宽度相同的字段中
格式是
cout.width(); 打印字段当前宽度;(这里默认是空格,其实也可以是别的字符填充,下面我们会看到)(其实我们知道默 认的字符宽度是0)
cout.width(int i) 打印i个空格;
width()函数是有返回值的,第一个函数返回的是当前字段的宽度;第二个函数返回以前的字段宽度值。
width函数默认右对齐。
注意 width函数只一次有效,如果需要,需要重复调用
以 The second test 为例子,cout.width(30)返回的是0,说明之前字符宽度是0(即默认字符宽度),我们可以数出它空出了30个 空格(可以数下面的横线)
三.填充字符
上面所说的空格可以用别的字符来填充,就是用下面的函数完成的
cout.fill(char a)
即用字符a来填充空格。
请看 The third test
四.设置浮点数的显示精度
格式是
cout.precision(int i)
对于浮点数,c++默认精度是6,和c不一样,这6位是总位数,而不是小数点后6位。
这个函数表示输出精度为i的小数(注意,i是总位数)
和width函数不同,但和fill函数类似,新的精度设置一直有效,直到被重新的设置。
请参考 The fourth test
五. 打印末尾的0和小数点
在c++中如果要输出3.14的6位精度数,其实还是3.14。也就是说,它不会像C语言那样补0。
可是有时,最后加上0就好看些,比如在价格或者是栏中的数字
格式是
cout.setf(ios_base::showpoint);
可以显示末尾小数点,对于精度要求,不足的补0
ios_base是一个类声明
六.关于setf()
(1.)
常量 | 含义 |
ios_base::boolalpha | 输入和输出bool值,可以为true或false |
ios_base::shoebase | 对于输出,使用c++基数前缀(0,0x) |
Ios_base::showpoint | 显示末尾小数点 |
ios_base::uppercase | 对于16进制输出,使用大写字母,E表示法 |
ios_base::showpos | 在正数前面加上+ |
另外以上函数都是一直有效的,直到被重新调整。如果想取消他们的作用,可以调用
cout.unsetf(......);
详情请接着往下看例子
(2.) setf(long,long)的参数
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二个参数 | 第一个参数 | 含义
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ios_base::dec | 使用基数10(转换成10进制)
------------------------------------------------------------------------------------------------------------------------------------------
ios_base::basefield | ios_base::oct | 使用基数8
--------------------------------------------------------------------------------------------------------------------------------------------
| ios_base::hex | 使用基数16
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ios_base::fixed | 使用定点计数法
ios_base::floatfield --------------------------------------------------------------------------------------------------------------------------------------------
| ios_base::scientific | 使用科学计数法
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ios_base::left | 使用左对齐
---------------------------------------------------------------------------------------------------------------------------------------------
ios_base::adjustfield | ios_base::right | 使用右对齐
---------------------------------------------------------------------------------------------------------------------------------------------
| ios_base::internal | 符号或基数前缀左对齐,值右对齐
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面是各种函数的实例以及运行结果,帮助大家理解
大部分都是一个函数一个函数来的
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
cout<<"********************************************************"<<endl;
cout<<"The first test: 进制转换 ***"<<endl;
cout<<"********************************************************"<<endl;
int a = 13;
cout<<"The value of a : "<<a<<endl;
cout<<hex;
cout<<"16 进制: "<<a<<endl;
cout<<oct;
cout<<"8 进制: "<<a<<endl;
cout<<dec;
cout<<"10 进制: "<<a<<endl;
cout<<endl;
cout<<"********************************************************"<<endl;
cout<<"The second test: 调整字符宽度 ***"<<endl;
cout<<"********************************************************"<<endl;
int w = cout.width(30);
cout<<w<<endl;
cout<<"- - - - - - - - - - - - - - - - - - - - - - -"<<endl;
cout.width(5);
cout<<'N'<<':';
cout.width(8);
cout<<"N * N"<<':'<<endl;
for(int i = 1; i <= 100; i *= 10){
cout.width(5);
cout<<i<<';';
cout.width(8);
cout<<i*i<<';'<<endl;
}
cout<<endl;
cout<<"********************************************************"<<endl;
cout<<"The third test: 填充字符 ***"<<endl;
cout<<"********************************************************"<<endl;
cout.fill('^');
cout.width(30);
cout<<"hehe"<<endl;
cout<<endl;
cout<<"********************************************************"<<endl;
cout<<"The fourth test: 精度要求 ***"<<endl;
cout<<"********************************************************"<<endl;
double b;
b = 3.14159265753;
cout<<"The value of b : "<<b<<endl;
cout.precision(3);
cout<<b<<endl;
cout.precision(10);
cout<<b<<endl;
cout<<setprecision(3)<<b<<endl<<endl; ///这个需要头文件 iomanip
cout<<"********************************************************"<<endl;
cout<<"The fifth test: 打印末尾的0和小数点 ***"<<endl;
cout<<"********************************************************"<<endl;
double c = 2.4;
cout<<"The value of c : "<<c<<endl;
cout.setf(ios_base::showpoint);
cout<<c<<endl;
cout.precision(5);
cout<<c<<endl;
cout.precision(1);
cout<<c<<endl;
cout.unsetf(ios_base::showpoint);
cout<<c<<endl<<endl;
cout<<"********************************************************"<<endl;
cout<<"关于setf(): ***"<<endl;
cout<<"********************************************************"<<endl;
cout<<"// ONE 输入输出bool值//"<<endl;
cout<<true<<' '<<!true<<endl;
cout.setf(ios_base::boolalpha);
cout<<true<<' '<<!true<<endl;
cout.unsetf(ios_base::boolalpha);
cout<<true<<' '<<!true<<endl<<endl;
cout<<"// TWO 使用基数前缀/// /"<<endl;
int d = 5;
cout<<"The value of d : "<<d<<endl;
cout.setf(ios_base::showbase);
cout<<d<<endl;
cout<<hex;
cout<<d<<' '<<a<<endl; /// a is from the first test. The value of a is 13
cout.unsetf(ios_base::showbase);
cout<<d<<' '<<a<<endl<<endl;;
cout<<"// THREE 16进制输出为大写字母/ /"<<endl;
int e = 63;
cout<<dec;
cout<<"The value of e is : "<<e<<endl;
cout<<hex;
cout<<e<<endl;
cout.setf(ios_base::uppercase);
cout<<e<<endl;
cout.unsetf(ios_base::uppercase);
cout<<e<<endl<<endl;
cout<<"// FOUR 正数前加+/ "<<endl;
int f = 97;
cout<<dec;
cout<<"The value of f is : "<<f<<endl;
cout.setf(ios_base::showpos);
cout<<f<<endl;
cout.unsetf(ios_base::showpos);
cout<<f<<endl<<endl;
cout<<"/ FIVE 进制转换"<<endl;
int g = 45;
cout<<"The value of g is : "<<g<<endl;
cout.setf(ios_base::hex,ios_base::basefield);
cout<<"16 进制 "<<g<<endl;
cout.setf(ios_base::oct,ios_base::basefield);
cout<<" 8 进制 "<<g<<endl;
cout.setf(ios_base::dec,ios_base::basefield);
cout<<"10 进制 "<<g<<endl<<endl;
cout<<"/ SIX 计数法///"<<endl;
double h = 3.14;
cout.precision(6); ///由于test5中调用的precision一直有效,所以要还原回去
cout<<"The value of i is : "<<h<<endl;
cout.setf(ios_base::fixed,ios_base::floatfield);
cout<< h <<endl;
cout.setf(ios_base::scientific,ios_base::floatfield);
cout<< h <<endl<<endl;
cout<<"/ SEVEN 格式对齐//"<<endl;
cout.precision(4);
cout<<fixed;
cout.fill(' ');
for(int i = 1; i <= 1000; i *= 10){
cout.width(4);
cout<<i*i;
cout.width(18);
cout<<sqrt(i)<<endl;
}
cout<<endl;
cout.setf(ios_base::left,ios_base::adjustfield);
for(int i = 1; i <= 1000; i *= 10){
cout.width(4);
cout<<i*i;
cout.width(18);
cout<<sqrt(i)<<endl;
}
cout<<endl;
cout.setf(ios_base::right,ios_base::adjustfield);
for(int i = 1; i <= 1000; i *= 10){
cout.width(4);
cout<<i*i;
cout.width(18);
cout<<sqrt(i)<<endl;
}
cout<<endl;
cout.setf(ios_base::internal,ios_base::adjustfield);
cout<<showpos;
for(int i = 1; i <= 1000; i *= 10){
cout.width(4);
cout<<i*i;
cout.width(18);
cout<<sqrt(i)<<endl;
}
cout<<endl;
return 0;
}