C++中cout的格式使用詳細(xì)介紹
1.cout和i/i++/++i的組合使用
i++ 和 ++i 是有著不同的含義,和 cout 組合使用也會(huì)得到不同的結(jié)果,下面給出一段代碼:
#include <iostream> using namespace std; int main(){ int i = 1; cout << ++i << i++ << i << i++ << ++i << endl; return 0; }
這段代碼的結(jié)果是多少呢?
A.23345
B.22335
C.54535
D.53525
…
我們不妨先理解一下 cout 輸出控制臺(tái)的過(guò)程??聪旅孢@幅圖:
根據(jù)表達(dá)式來(lái)看, endl 會(huì)作為一個(gè)可以供 cout 接收的對(duì)象往前傳,而 ++i 和 endl 結(jié)合起來(lái)作為一個(gè)可以供 cout 接收的對(duì)象往前傳,依次遞推下去。物理實(shí)現(xiàn)上需要一個(gè)棧來(lái)保存可以供 cout 接收的對(duì)象,然后從右向左放到這個(gè)棧里,然后依次彈出輸出在屏幕上。其中, i 和 ++i 會(huì)在棧里面保存 i 的引用,而 i++ 會(huì)在棧里面保存數(shù)字,過(guò)程如下:
第一步:將 endl 壓入棧中, i 值不變;
第二步:將 i 的引用壓入棧中, i 的值加 1 變成 2(因?yàn)槭?++i );
第三步:將 2 壓入棧中, i 的值加 1 變成 3(因?yàn)槭?i++ );
第四步:將 i 的引用壓入棧中, i 的值不變(因?yàn)槭?i );
第五步:將 3 壓入棧中, i 的值加 1 變成 4(因?yàn)槭?i++ );
第六步:將 i 的引用壓入棧中, i 的值加 1 變成 5(因?yàn)槭?++i );
第七步:將棧里的數(shù)據(jù)依次彈出,即可得到 53525 。(因?yàn)閕的值是 5 ,所以所有 i 的引用都是 5 )
2.用不同進(jìn)制輸出數(shù)字
方法一:用控制符 dec(十進(jìn)制),hex(十六進(jìn)制),oct(八進(jìn)制)
#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(); //有些設(shè)備的運(yùn)行窗口只彈出一下,加上該句可以讓窗口停留 return 0; }
運(yùn)行結(jié)果:
在默認(rèn)情況下,cout以十進(jìn)制格式顯示整數(shù)
其中,oct 是八進(jìn)制輸出, dec 是十進(jìn)制(效果和默認(rèn)一樣), hex 是十六進(jìn)制輸出(字母默認(rèn)是小寫(xiě)字母)。這兩個(gè)也包含在 std 中,即其全稱分別是 std::oct 、 std::dec 、 std::hex ,這三個(gè)控制符包含在庫(kù) < iostream > 中。
注意:默認(rèn)格式為十進(jìn)制,在修改格式之前,原來(lái)的格式將一直有效。(修改后的格式一直有效,直到更改它為止)
方法二:使用setbase(n)
#include <iostream> #include <iomanip> using namespace std; int main(){ int i = 123456; cout << i << endl; cout << dec << i << endl; cout <<"八進(jìn)制:" << oct << i << endl; cout <<"十六進(jìn)制(小寫(xiě)字母):" << hex << i << endl; cout << setiosflags(ios::uppercase); cout <<"十六進(jìn)制(大寫(xiě)字母):" << hex << i << endl; cout <<"八進(jìn)制:" << setbase(8) << i << endl; cout <<"十六進(jìn)制:" << setbase(16) << i << endl; }
setbase(n) 表示以 n 進(jìn)制顯示,包含在庫(kù) < iomanip > 中,n 只能取 8, 10, 16 三個(gè)值。
setiosflags(ios::uppercase) 表示將字母大寫(xiě)輸出,包含在庫(kù) < iomanip > 中。
以上均包含在std 命名空間中。
3.調(diào)整字段寬度
width函數(shù)將長(zhǎng)度不同的數(shù)字放到寬度相同的字段中,該方法原型為:
int width()
int width(int i)
第一種格式返回字段寬度的當(dāng)前設(shè)置;第二種格式將字段寬度設(shè)置為i個(gè)空格,并返回以前的字段寬度值。這使得能夠保存以前的值以便恢復(fù)寬度值時(shí)使用。
注意:width()方法只影響接下來(lái)顯示的一個(gè)項(xiàng)目,然后字段寬度將恢復(fù)為默認(rèn)值。
#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; //返回當(dāng)前字段的寬度 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)來(lái)設(shè)置輸出域?qū)挕?/p>
#include <iostream> #include <iomanip> using namespace std; int main(){ cout << 's' << setw(8) << 'a' << endl; //s與a之間有7個(gè)空格 cout << 's' << setw(3) << 'abcd' << endl; }
setw()只對(duì)其后面緊跟的輸出產(chǎn)生作用,如上例中,表示'a'共占8個(gè)位置,不足的7個(gè)位置用空格填充。
若緊跟的輸出的內(nèi)容超過(guò)setw()設(shè)置的長(zhǎng)度,輸出結(jié)果存在問(wèn)題。
填充字符
在默認(rèn)情況下,cout用空格填充字段中未被使用的部分,可以用fill()成員函數(shù)來(lái)改變填充字符。
控制符包含在庫(kù) < 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.設(shè)置浮點(diǎn)數(shù)的顯示精度
方法一:
C++的默認(rèn)精度為六位(但末尾的0將不顯示)。precision()成員函數(shù)使得能夠選擇其他值,與fill類(lèi)似,新的精度設(shè)置將一直有效,直到被重新設(shè)置。
#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; }
方法二:
前提:包含庫(kù) < iomanip > ,這個(gè)庫(kù)包含了對(duì)輸入輸出的控制。
#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++默認(rèn)浮點(diǎn)數(shù)輸出有效位數(shù)是 6 位(若前面整數(shù)位數(shù)大于 6 位,使用科學(xué)計(jì)數(shù)法輸出),而通過(guò)以下幾種方式可以更改輸出精度:
1.使用 setprecision(n) 即可設(shè)置浮點(diǎn)數(shù)輸出的有效位數(shù)
(若前面整數(shù)位數(shù)大于 n 位,使用科學(xué)計(jì)數(shù)法輸出)
2.使用 setiosflags(ios::fixed) 或 fixed,表示對(duì)小數(shù)點(diǎn)后面數(shù)字的輸出精度進(jìn)行控制
所以,和 setprecision(n) 結(jié)合使用即可設(shè)置浮點(diǎn)數(shù)小數(shù)點(diǎn)后面數(shù)字的輸出精度,位數(shù)不足的補(bǔ)零
以上均采用 “四舍五入” 的方法控制精度,三個(gè)控制符均包含在 std 命名空間中。
打印末尾的0和小數(shù)
#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的格式使用的詳細(xì)內(nèi)容,更多關(guān)于C++ cout格式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
常用的C++標(biāo)準(zhǔn)庫(kù)頭文件小結(jié)
C++標(biāo)準(zhǔn)庫(kù)定義了一系列函數(shù)、宏和對(duì)象,以實(shí)現(xiàn)跨團(tuán)隊(duì)、跨平臺(tái)的高效且具有卓越性能的標(biāo)準(zhǔn)化 C++ 代碼, 本文介紹常用的C++標(biāo)準(zhǔn)庫(kù)頭文件,需要的朋友可以參考下2023-11-11關(guān)于python調(diào)用c++動(dòng)態(tài)庫(kù)dll時(shí)的參數(shù)傳遞問(wèn)題
這篇文章主要介紹了python調(diào)用c++動(dòng)態(tài)庫(kù)dll時(shí)的參數(shù)傳遞,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04C++使用Kruskal和Prim算法實(shí)現(xiàn)最小生成樹(shù)
這篇文章主要介紹了C++使用Kruskal和Prim算法實(shí)現(xiàn)最小生成樹(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之二分法查找詳解
二分查找算法是在有序數(shù)組中用到的較為頻繁的一種算法,在未接觸二分查找算法時(shí),最通用的一種做法是,對(duì)數(shù)組進(jìn)行遍歷,跟每個(gè)元素進(jìn)行比較,其時(shí)間為O(n),但二分查找算法更優(yōu)2022-02-02C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08