C++中cout的格式使用详细介绍

1.cout和i/i++/++i的组合使用

i++ 和 ++i 是有着不同的含义,和 cout 组合使用也会得到不同的结果,下面给出一段代码:

#include <iostream>using namespace std;int main(){    int i = 1;    cout << ++i << i++ << i << i++ << ++i << endl;    return 0;}

这段代码的结果是多少呢?A.23345B.22335C.54535D.53525…我们不妨先理解一下 cout 输出控制台的过程。看下面这幅图:

根据表达式来看, endl 会作为一个可以供 cout 接收的对象往前传,而 ++i 和 endl 结合起来作为一个可以供 cout 接收的对象往前传,依次递推下去。物理实现上需要一个栈来保存可以供 cout 接收的对象,然后从右向左放到这个栈里,然后依次弹出输出在屏幕上。其中, i 和 ++i 会在栈里面保存 i 的引用,而 i++ 会在栈里面保存数字,过程如下:

第一步:将 endl 压入栈中, i 值不变;第二步:将 i 的引用压入栈中, i 的值加 1 变成 2(因为是 ++i );第三步:将 2 压入栈中, i 的值加 1 变成 3(因为是 i++ );第四步:将 i 的引用压入栈中, i 的值不变(因为是 i );第五步:将 3 压入栈中, i 的值加 1 变成 4(因为是 i++ );第六步:将 i 的引用压入栈中, i 的值加 1 变成 5(因为是 ++i );第七步:将栈里的数据依次弹出,即可得到 53525 。(因为i的值是 5 ,所以所有 i 的引用都是 5 )

2.用不同进制输出数字

方法一:用控制符 dec(十进制),hex(十六进制),oct(八进制)

#include <iostream>using namespace std;int main(){ int chest = 42;     // decimal integer literal    int waist = 0x42;   // hexadecimal integer literal    int inseam = 042;   // octal integer literal    cout << "Monsieur cuts a striking figure!\n";    cout << "chest = " << chest << " (42 in decimal)\n";    cout << "waist = " << waist << " (0x42 in hex)\n";    cout << "inseam = " << inseam << " (042 in octal)\n";        chest = 42;    waist = 42;     inseam = 42;    cout << "Monsieur cuts a striking figure!"  << endl;    cout << "chest = " << chest << " (decimal for 42)" << endl;    cout << hex;      // manipulator for changing number base    cout << "waist = " << waist << " (hexadecimal for 42)" << endl;    cout << oct;      // manipulator for changing number base    cout << "inseam = " << inseam << " (octal for 42)" << endl;    // cin.get();                    //有些设备的运行窗口只弹出一下,加上该句可以让窗口停留    return 0; }

运行结果:

在默认情况下,cout以十进制格式显示整数

其中,oct 是八进制输出, dec 是十进制(效果和默认一样), hex 是十六进制输出(字母默认是小写字母)。这两个也包含在 std 中,即其全称分别是 std::oct 、 std::dec 、 std::hex ,这三个控制符包含在库 < iostream > 中。

注意:默认格式为十进制,在修改格式之前,原来的格式将一直有效。(修改后的格式一直有效,直到更改它为止)

方法二:使用setbase(n)

#include <iostream>#include <iomanip>using namespace std;int main(){int i = 123456;cout << i << endl;cout << dec << i << endl;cout <<"八进制:" << oct << i << endl;cout <<"十六进制(小写字母):" << hex << i << endl;cout << setiosflags(ios::uppercase);cout <<"十六进制(大写字母):" << hex << i << endl;cout <<"八进制:" << setbase(8) << i << endl;cout <<"十六进制:" << setbase(16) << i << endl;}

setbase(n) 表示以 n 进制显示,包含在库 < iomanip > 中,n 只能取 8, 10, 16 三个值。setiosflags(ios::uppercase) 表示将字母大写输出,包含在库 < iomanip > 中。以上均包含在std 命名空间中。

3.调整字段宽度

width函数将长度不同的数字放到宽度相同的字段中,该方法原型为:

int width()

int width(int i)

第一种格式返回字段宽度的当前设置;第二种格式将字段宽度设置为i个空格,并返回以前的字段宽度值。这使得能够保存以前的值以便恢复宽度值时使用。

注意:width()方法只影响接下来显示的一个项目,然后字段宽度将恢复为默认值。

#include <iostream>using namespace std;int main(){    int w = cout.width(30);    cout << "default field width = " << w << ":\n";    int a;    cout.width(10);cout <<cout.width(10) <<endl;//返回当前字段的宽度      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";    }    return 0; }

也可以使用setw(int n)来设置输出域宽。

#include <iostream>#include <iomanip>using namespace std;int main(){cout << 's' << setw(8) << 'a' << endl;//s与a之间有7个空格 cout << 's' << setw(3) << 'abcd' << endl;}

setw()只对其后面紧跟的输出产生作用,如上例中,表示’a’共占8个位置,不足的7个位置用空格填充。

若紧跟的输出的内容超过setw()设置的长度,输出结果存在问题。

填充字符

在默认情况下,cout用空格填充字段中未被使用的部分,可以用fill()成员函数来改变填充字符。

控制符包含在库 < iomanip > 中,std 命名空间中。

#include <iostream>using namespace std;int main(){    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";    }    return 0; }

注意:与字段宽度不同的是,新的填充字符将一直有效,直到更改它为止。

4.设置浮点数的显示精度

方法一:

C++的默认精度为六位(但末尾的0将不显示)。precision()成员函数使得能够选择其他值,与fill类似,新的精度设置将一直有效,直到被重新设置。

#include <iostream>using namespace std;int main(){    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";    return 0; }

方法二:

前提:包含库 < iomanip > ,这个库包含了对输入输出的控制。

#include <iostream>#include <iomanip>using namespace std;int main(){double i = 3333.1415926;cout << i << endl;cout << setprecision(3) << i << endl;cout << setprecision(9) << i << endl;cout << setiosflags(ios::fixed);cout << i << endl;cout << fixed << setprecision(3) << i << endl;cout << setprecision(9) << fixed <<  i << endl;}

可以看出,C++默认浮点数输出有效位数是 6 位(若前面整数位数大于 6 位,使用科学计数法输出),而通过以下几种方式可以更改输出精度:1.使用 setprecision(n) 即可设置浮点数输出的有效位数(若前面整数位数大于 n 位,使用科学计数法输出)2.使用 setiosflags(ios::fixed) 或 fixed,表示对小数点后面数字的输出精度进行控制所以,和 setprecision(n) 结合使用即可设置浮点数小数点后面数字的输出精度,位数不足的补零以上均采用 “四舍五入” 的方法控制精度,三个控制符均包含在 std 命名空间中。

打印末尾的0和小数

#include <iostream>using namespace std;int main(){    float price1 = 20.40;    float price2 = 1.9 + 8.0 / 9.0;     cout.setf(ios_base::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; }

以上就是C++中cout的格式使用的详细内容,更多关于C++ cout格式的资料请关注其它相关文章!

你所缺少的部分,也早已被我用想像的画笔填满。

C++中cout的格式使用详细介绍

相关文章:

你感兴趣的文章:

标签云: