目录
scientific--->固定以科学计数法显示浮点数,永久生效
简单了解cout是什么?
cout是ostream类的一个对象,叫做标准输出流对象;cin是istream类的一个对象,叫做标准输入流对象;iostream类继承了ostream和istream;当程序包含<iostream>时,自动在命名空间std中创建对象cout和cin。
什么是字节流
字节流大概就是一个个字节组成的队列。
c++的输入输出是面向字节流的。输入时,程序从输入流中取数据;输出时,程序将数据插入到输出流中。可以理解为c++输入输出的单位是单个字节。
int a = 123;
cout << a;
此处 a 是 int 类型,占4个字节,底层是32个二进制位。如果一个字节一个字节的取数据,不可能取到1 2 3这三个数,然后插入输出流进而显示到显示器上。ostream类将数据的底层二进制转化为由一个个字符字节组成的输出流。 理解这一点很重要。
默认格式控制
- 对于char值,如果它代表的是可打印字符,则将被作为一个字符显示在宽度为一个字符的字段中。
- 对于数值整型,将以十进制方式显示在一个刚好容纳该数字及负号(如果有的话)的字段中。
- 字符串被显示在宽度等于该字符串长度的字段中。
- 浮点类型被显示为6位,末尾的0不显示(注意,显示的数字位数与数字被存储时精度没有任何关系)。数字以定点表示法显示还是以科学计数法表示,取决于它的值。当指数大于等于6或小于等于-5时,将使用科学计数法表示。另外,字段宽度恰好容纳数字和负号(如果有的话)。
#include <iostream>
int main()
{
using std::cout;
cout << "12345678901234567890\n";
char ch = 'K';
int t = 273;
cout << ch << ":\n";
cout << t << ":\n";
cout << -t <<":\n";
double f1 = 1.200;
cout << f1 << ":\n";
cout << (f1 + 1.0 / 9.0) << ":\n";
double f2 = 1.67E2;
cout << f2 << ":\n";
f2 += 1.0 / 9.0;
cout << f2 << ":\n";
cout << (f2 * 1.0e4) << ":\n";
double f3 = 2.3e-4;
cout << f3 << ":\n";
cout << f3 / 10 << ":\n";
// std::cin.get();
return 0;
}
修改计数系统
#include <iostream>
int main()
{
using namespace std;
cout << "Enter an integer: ";
int n;
cin >> n;
cout << "n n*n\n";
cout << n << " " << n * n << " (decimal)\n";
// set to hex mode
cout << hex; //以16进制输出
cout << n << " ";
cout << n * n << " (hexadecimal)\n";
// set to octal mode
cout << oct << n << " " << n * n << " (octal)\n";//以八进制输出
// alternative way to call a manipulator
dec(cout);//以十进制输出
cout << n << " " << n * n << " (decimal)\n";
// cin.get();
// cin.get();
return 0;
}
代码说明:
- ostream重载了<<运算符,cout<<hex等价于hex(cout)
- hex(cout)场景下,hex看做是一个函数
- cout << hex场景下,hex看做一个输出控制符;这一点与flush一个道理(不了解flush的可以看如何刷新缓冲区(c++、c、linux)-CSDN博客)
- 后续的oct、dec同样的道理
- 一旦使用计数控制符(hex、oct、hec)除非下次修改,否则一直处在这个状态
调整字符宽度
int width(int i):将字符宽度设置为i个空格,并返回原始默认字符宽度。需要简单说明的是:width是iostream类的一个成员函数。注意:width()只影响下一次操作,紧接着就变为默认值。 c++中字符宽度默认值是0。在输出时真实字符宽度,会选择设置字符宽度与完全显示所需字符宽度,的最大值。
#include <iostream>
int main()
{
using std::cout;
int w = cout.width(30);
cout << "default field width = " << w << ":\n";
cout.width(5);
cout << "N" <<':';
cout.width(8);
cout << "N * N" << ":\n";
for (long i = 1; i <= 100; i *= 10)
{
cout.width(5);
cout << i <<':';
cout.width(8);
cout << i * i << ":\n";
}
// std::cin.get();
return 0;
}
填充字符
在默认情况下,cout用空格填充字段中未被使用的部分,可以用fill()成员函数来改变填充字符。cout.fill()会影响后续所有操作
#include <iostream>
int main()
{
using std::cout;
cout.fill('*');
const char * staff[2] = { "Waldo Whipsnade", "Wilmarie Wooper"};
long bonus[2] = {900, 1350};
for (int i = 0; i < 2; i++)
{
cout << staff[i] << ": $";
cout.width(7);
cout << bonus[i] << "\n";
}
// std::cin.get();
return 0;
}
设置浮点数显示精度
这里的浮点数显示精度是指:显示的总位数。c++默认的浮点数显示精度为6位,末尾的0不显示。precision()成员函数用于修改浮点数显示精度,末尾的0同样不显示。永久有效
#include <iostream>
int main()
{
using std::cout;
float price1 = 20.40;
float price2 = 1.9 + 8.0 / 9.0;
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout.precision(2);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout.precision(7);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
// std::cin.get();
return 0;
}
打印末尾的0和小数点
格式控制符:showpoint,用于显示末尾的0和小数点,永久有效。
#include <iostream>
int main()
{
using std::cout;
// using std::ios_base;
float price1 = 20.40;
float price2 = 1.9 + 8.0 / 9.0;
// cout.setf(ios_base::showpoint);不建议使用setf
cout << showpoint;
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout.precision(2);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
// std::cin.get();
return 0;
}
其他格式控制符
right--->设置为右对齐,永久生效
c++默认就是右对齐
#include <iostream>
using namespace std;
int main()
{
using std::cout;
int a = 1234567;
int b = 1;
int c = 2;
cout.width(7);
cout << right;
cout << b << endl;
cout.width(7);
cout << c << endl;
cout.width(7);
cout << a << endl;
return 0;
}
left--->设置为左对齐,永久生效
#include <iostream>
using namespace std;
int main()
{
using std::cout;
cout.fill('#');
int a = 1234567;
int b = 1;
int c = 2;
cout.width(7);
cout << left;
cout << b << endl;
cout.width(7);
cout << c << endl;
cout.width(7);
cout << a << endl;
return 0;
}
fixed--->固定以小数显示浮点数,永久生效
#include <iostream>
using namespace std;
int main()
{
using std::cout;
cout << fixed;
double a = 0.000012;
double b = 12345678.9;
cout << a << endl;
cout << b << endl;
return 0;
}
scientific--->固定以科学计数法显示浮点数,永久生效
#include <iostream>
using namespace std;
int main()
{
using std::cout;
double a = 0.12;
double b = 12.9;
cout << a << endl;
cout << b << endl;
cout << scientific;
cout << a << endl;
cout << b << endl;
return 0;
}
其它格式控制符不再赘述。
大家很明显感受到,用格式控制符来进行格式化输出,更舒服一点。在前面的介绍中,cout.width()、cout.precision()、cout.fill()都是使用cout对象的成员函数来进行格式化输出。在<iomanip>中为它们三个分别设置了相应的控制符。
<iomanip>
- setprecision()用于控制浮点数显示精度,永久有效
- setfill()用于控制填充字符,永久有效
- setw()用于控制字符宽度,只生效一次
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
using namespace std;
// use new standard manipulators
cout << fixed << right;
// use iomanip manipulators
cout << setw(6) << "N" << setw(14) << "square root"
<< setw(15) << "fourth root\n";
double root;
for (int n = 10; n <=100; n += 10)
{
root = sqrt(double(n));
cout << setw(6) << setfill('.') << n << setfill(' ')
<< setw(12) << setprecision(3) << root
<< setw(14) << setprecision(4) << sqrt(root)
<< endl;
}
// std::cin.get();
return 0;
}