C++ cout的使用,看这一篇就够了
1 C++輸入輸出綜述
C++ IO首先建立在為Unix環境開發的原始庫函數上;ANSI C正式承認這個庫時,將其稱為標準輸入/輸出包;
IO相關類定義在頭文件iostream和fstream,這些類不是正式語言定義的組成部分,cin,istream都不是關鍵字。
1.1 流和緩沖
(1)流簡介:
- C++程序將輸入和輸出看作字符流;對于輸入來說,程序從輸入流中抽取字符,對于輸出來說,程序向輸出流中插入字符;
- 輸入流可以來自鍵盤、存儲設備或者其他程序;輸出流可以輸出至顯示器、打印機、存儲設備或者其他程序。
- 流是程序與流源或流目的之間的中介,這樣C++就可以對來源不同的字符做相同處理。
(2)管理輸入:
- 兩個階段:將流與程序綁定在一起,將流與源綁定在一起
(3)管理輸出:
- 兩個階段:將流與目的綁定在一起,將流與程序綁定在一起
(4)緩沖區簡介
緩沖區就是一塊存儲空間,它是為了匹配程序處理速度和外設處理速度;比如程序一次處理1byte,但是磁盤一次讀取512bytes;又或者程序一次處理1byte,可以1byte地從磁盤讀取,但是由于硬件讀取一次數據復雜且操作慢,因此使用緩沖區可以加快程序處理速度。
flushing the buffer:刷新緩沖區就是清空緩沖區地內容以備下次使用。
1.2 輸入輸出中比較重要的類
- streambuf:提供緩沖區,有成員方法 填滿緩沖區、獲取緩沖區內容、刷新緩沖區、管理緩沖區
- ios_base:表示流的一般屬性 比如文件是否打開、是二進制流還是文本流等等
- ios:基于ios_base,并且它包含了一個指針成員指向一個streambuf對象
- ostream:繼承自ios類并提供了輸出方法
- istream:繼承自ios類并提供了輸入方法
- iostream:繼承自ostream類和istream類
1.3 C++11 I/O新特性
- ostream.h轉換為ostream,將ostream類放置到std命名空間中
- I/O類被重寫,開發了I/O類模板包括basic_istream<charT,traits>和basic_ostream<charT, traits>。實現了char,wchar_t具體化;istream和ostream是char的具體化,cout輸出字符流,wistream和wstream是wchar_t的具體化,wcout用于輸出寬字符流。
- ios基類中的一些獨立與類型的信息被移動到ios_base類中,比如格式化常量ios::fixed變為ios_base::fixed,還新增了一些常量
1.4 包含iostream頭文件時會自動創建八個流對象(4個用于窄字符流,4個用于寬字符流)
- cin對象:對應標準輸入流,默認情況下這個流與標準輸入設備匹配(鍵盤);wcin對象用于wchar_t類型;
- cout對象:對應標準輸出流,默認情況下這個流與標準輸出設備匹配(顯示器),借助streambuf管理流;wcout對象用于wchar_t類型;
- cerr對象:對應于標準錯誤流(可以用于顯示錯誤信息),默認情況下這個流與標準輸出設備匹配(顯示器),這個流是不緩沖的;wcerr對象用于wchar_t類型;不受重定向的影響,即使重定向了輸入輸出流,錯誤信息還是打印到顯示器上
- clog對象:對應于標準錯誤流,默認情況下這個流與標準輸出設備匹配(顯示器),這個流是緩沖的;wclog對象用于wchar_t類型。不受重定向的影響,即使重定向了輸入輸出流,錯誤信息還是打印到顯示器上
1.4 重定向
修改標準輸入和標準輸出關聯的工具。(比如輸出到文件,而不是顯示器)
2 cout的使用
當創建cout類時會自動打開一個流,創建一個緩沖區,并將流和緩沖區聯系起來
C++在輸出時將數據看作字符流。
2.1 顯示各種類型的數據
各種char字符可以直接顯示,但是對于各種數據包括int,long,float,double就無法直接解析為字符了
C++中將使用cout的<<稱為插入操作符,意味著將右側的信息流傳遞到左側(因為<<是左移運算符,相當于給了個方便記憶的解釋)
因此cout類重載了<<操作符,重載了適合多種數據類型的<<操作符成員函數,將數據類型轉換為文本形式的字符流;以下數據類型皆被重載:unsigned char、signed char、char、short、unsigned short、int、unsigned int、long、unsigned long、long long (C++11)、unsigned long long (C++11)、float、double、long double
重載原型為:ostream & operator<<(type);
2.2 輸出與指針
ostream為const signed char *、const unsigned char *、const char *、void *重載了<<操作符,因此,可以使用cout<<輸出顯示字符串;這個方法使用\0來判斷是否停止顯示字符。
如果要顯示字符串的地址,由于傳遞指針輸出了整個字符串,因此將其強制轉換為void *類型可以顯示字符串的地址。
舉例:
cout << "輸出與指針*********************************************************************" << endl; int eggs = 12; const char* amount = "dozen"; cout << &eggs; // prints address of eggs variable cout << amount; // prints the string "dozen" cout << (void*)amount<<endl; // prints the address of the "dozen" string運行結果:
輸出與指針********************************************************************* 008FFD9Cdozen0041BC802.3 輸出連接
ostream & operator<<(type);
根據上述函數原型可以知道,返回值是ostream類型的引用,這就可以使得cout<<實現輸出連接
cout << “We have " << count << " unhatched chickens.\n”;
舉例:
cout << "輸出連接***********************************************************************" << endl; cout << "We have " << eggs << " unhatched chickens.\n";運行結果:
輸出連接*********************************************************************** We have 12 unhatched chickens.2.4 輸出字符cout.put()
原型:
template <class type>//type為char或wchar_t ostream & put(type);當傳遞的實參不是char或wchar_t時,會執行自動轉換將其轉換為char或wchar_t類型,這個很適合早期發行版本2.0,在那些版本中,使用int值表示字符。
cout << 'W';//在早期發行版本2.0,這個會輸出W的ASCII碼,當前版本會輸出字符W cout.put('W');//在早期發行版本2.0,這個會輸出W字符,當前版本會輸出W字符一些編譯器為char, unsigned char, and signed char重載了put(),但是這會給自動轉換int實參帶來歧義,因為int可以轉換為其中任何一個。
舉例:
cout << "cout.put()*********************************************************************" << endl; cout.put('W'); // display the W character cout.put('I').put('t'); // displaying It with two put() calls cout.put(65); // display the A character cout.put(66.3); // display the B character cout.put(65); // display the A character cout.put(66.3) << endl; // display the B character運行結果:
cout.put()********************************************************************* WItABAB2.5 輸出字符串cout.write()
原型:
basic_ostream<charT,traits>& write(const char_type* s, streamsize n);第一個參數提供了需要顯示的字符串地址,第二個參數指示了顯示幾個字符;cout.write()成員函數不會判斷字符串是否結尾,指定顯示幾個字符就顯示幾個,即使它過界了。返回值為cout,說明支持輸出連接
針對write非char類型的數據,程序根據8bit存儲空間的內容來決定顯示為哪個字符,超過8bit的數據類型,則分為幾個字符顯示。
舉例:
cout << "cout.write()*******************************************************************" << endl; const char* state1 = "Florida"; const char* state2 = "Kansas"; const char* state3 = "Euphoria"; int len = std::strlen(state2); cout << "Increasing loop index:\n"; int i; for (i = 1; i <= len; i++) { cout.write(state2, i); cout << endl; } // concatenate output cout << "Decreasing loop index:\n"; for (i = len; i > 0; i--) cout.write(state2, i) << endl; // exceed string length cout << "Exceeding string length:\n"; cout.write(state2, len + 5) << endl;long val = 560031841; cout.write((char*)&val, sizeof(long))<<endl;運行結果:
cout.write()******************************************************************* Increasing loop index: K Ka Kan Kans Kansa Kansas Decreasing loop index: Kansas Kansa Kans Kan Ka K Exceeding string length: KansasEup aha!2.6 清空輸出緩存
緩存一般是512bytes或者其倍數。
在顯示器輸出時,三種情況可清空緩存
1.發送一個新行給緩沖器,endl
2.當輸入掛起時
3.使用工具 flush()函數
使用方法:flush(cout);或者cout<<flush;(這個重載了<<操作符)
舉例:
cout << "清空輸出緩存*******************************************************************" << endl; cout << "Enter a number: "; float num; cin >> num; cout << "Hello, good-looking! " << flush; cout << "Wait just a moment, please." << endl;運行結果:
清空輸出緩存******************************************************************* Enter a number: 99 Hello, good-looking! Wait just a moment, please.2.7 cout針對不同數據類型的輸出格式
(1)char:占用一個字符的位置
(2)整形:占用足夠顯示它的字符位置數(十位數占用兩個字符位置,個位數占用一個字符位置),如果是負數要在前面加一個-號
(3)字符串:占用字符串長度的字符位置數
(4)浮點型:顯示的有效位數為6位,尾部的0不顯示
注意事項:不同地區可能有不同的格式:比如歐洲的小數點用,表示而不是.表示;數據之間需要手動添加間隔符將兩個數字分開,否則會連續顯示。
舉例:
cout << "cout針對不同數據類型的輸出格式***************************************************" << endl; 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";運行結果:
cout針對不同數據類型的輸出格式*************************************************** 12345678901234567890 K: 273: -273: 1.2: 1.31111: 167: 167.111: 1.67111e+06: 0.00023: 2.3e-05:2.8 cout以不同進制顯示整數
ios_base是ios的基類,ios是一個模板類包含了char和wchar_t的專門化,ios_base包含了非模板的特征
流操縱符dec,hex,oct:ios_base中有工具dec,hex,oct使得數據以不同進制顯示;dec(),hex(),oct()是函數,并且重載了<<操作符;這幾個工具在std命名空間中
注意事項:
1.無法使用二進制顯示;
2.在更改進制顯示方式之后,系統默認后面使用該方式顯示數據,如果有顯示為其他進制形式的需要重新設置進制顯示方式
三種格式:
hex(cout);
cout<<hex;
cout.setf(ios_base::dec);
顯示bool類型:
cout.setf(ios_base::boolalpha);//true顯示位true,false顯示位false
舉例:
cout << "cout以不同進制顯示整數***********************************************************" << endl; /*cout以不同進制顯示整數*/ i = 90; cout << "以十進制顯示i:" << dec << i << endl;//以十進制顯示 cout << "以八進制顯示i:" << oct << i << endl;//以八進制顯示 cout << "以16進制顯示i:" << hex << i << endl;//以16進制顯示 //如需更改為十進制顯示方式,則可以使用以下方式 cout.setf(ios_base::dec); cout << "以十進制顯示i:" << i << endl << endl; /*bool數據類型顯示*/ cout << "bool數據類型顯示" << endl; bool is_true = true; cout.setf(ios_base::boolalpha);//可以顯示為true或false cout << "is_true = " << is_true << endl; is_true = false; cout << "is_true = " << is_true << endl << endl;運行結果:
cout以不同進制顯示整數*********************************************************** 以十進制顯示i:90 以八進制顯示i:132 以16進制顯示i:5a 以十進制顯示i:90bool數據類型顯示 is_true = true is_true = false2.9 設置當前字段寬度cout.width()
兩種原型:
int width();//返回當前字段寬度
int width(int i);//將字段寬度設置為i,并且返回之前的字符寬度,以備返回上一步的字段寬度。
使用格式:
cout<<width(長度);
注意事項:
1.width()只影響下一次的cout,然后字段寬度將恢復默認值
2.顯示為右對齊,多余的字段位置將使用空格填充
3.C++不會截斷數據,因此如果視圖在寬度為2個字段中打印一個7位數,C++將增寬字段,以容納該數據。
舉例:
cout << "width()***************************************************************************" << endl; 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"; } cout.width(5); cout << 9.888889999 << endl;//將不會截斷運行結果:
width()***************************************************************************default field width = 0:N: N * N:1: 1:10: 100:100: 10000: 9.888892.10 更改填充字符cout.fill()
默認情況下,將空格字符作為填充字符,可以使用fill()函數更改填充字符。
cout.fill('*');這對于打印支票很有用,因為這樣就不能隨意在填充部分隨意加數字了。
注意事項:在更改填充字符之后,系統默認后面使用該填充字符,如果有顯示為其他填充字符的需要重新設置填充字符
舉例:
cout << "更改填充字符****************************************************************************" << endl; 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"; }運行結果:
更改填充字符**************************************************************************** Waldo Whipsnade: $****900 Wilmarie Wooper: $***13502.11 更改浮點數顯示精度cout.precision()
默認情況下是顯示六位有效數字;可以使用precision()設置精度;
cout.precision(2);
注意事項:
1.在設置精度后,后面會一直使用這個精度直到下一次更改精度
2.當有效數字位數小于整數有效數字位數時,使用科學計數法顯示
舉例:
cout << "設置小數顯示精度:precision()***********************************************************" << endl; double j = 3333.1415926; /*設置顯示有效數字位數*/ cout << "默認情況顯示(6位):" << j << endl;//輸出浮點數時,默認情況下保留六位有效數字 cout.precision(9);//可以使用cout.precision(n)設置輸出浮點數時保留n位有效數字 cout << "設置有效數字位數為9位時:" << j << endl; cout.precision(3);//當有效數字位數小于整數有效數字位數時,使用科學計數法顯示 cout << "設置有效數字位數為3位時:" << j << endl << endl;運行結果:
設置小數顯示精度:precision()*********************************************************** 默認情況顯示(6位):3333.14 設置有效數字位數為9位時:3333.14159 設置有效數字位數為3位時:3.33e+032.12 ios_base成員函數setf()的使用
ios_base類中有一個受保護的數據成員,這個數據成員的各位控制著格式化的各個方面(這些位置1則打開該模式);
ios_base類中定義了各種常量指向該受保護數據成員的各個類,稱為bitmask類型,每個bitmask位都是可單獨訪問的并且有自己的含義,iostream軟件包使用bitmask來存儲狀態信息
| ios_base::showbase | 輸出時使用前綴 OX,ox(進制縮寫)等等 |
| ios_base::showpoint | 輸出小數點和無效0(就尾巴上的0) |
| ios_base::uppercase | 輸出16進制時使用大寫,輸出使用科學計數法時使用大寫E |
| ios_base::showpos | 在正數前面加+號,只適用于十進制,其他進制不用顯示+號 |
setf原型1:形參為1個
fmtflags setf(fmtflags);//輸入參數為bitmask類型,指出要設置哪一位;返回值為bitmask類型,指出以前的設置self原型2:形參為兩個
fmtflags setf(fmtflags , fmtflags );//第一個參數為bitmask類型,指出要設置哪一位;第二個參數為bitmask類型,指出要清除第一個參數中的哪些位;返回值為bitmask類型,指出以前的設置應用:
cout.setf(ios_base::hex, ios_base::basefield);//取消ios_base::basefield原來的設置,將其設置為hex可以傳遞的參數為以下表格:
| ios_base::basefield | ios_base::dec | 使用十進制 |
| ios_base::oct | 使用八進制 | |
| ios_base::hex | 使用十六進制 | |
| ios_base::floatfield | ios_base::fixed | 使用混合顯示模式 |
| ios_base::scientific | 使用科學計數法顯示模式 | |
| ios_base::adjustfield | ios_base::left | 左對齊 |
| ios_base::right | 右對齊 | |
| ios_base::internal | 符號左對齊,數字右對齊 |
注意事項:在C++標準中,混合模式和科學計數法模式都有兩個特征:
1.精度是指小數點后面的數據位數
2.尾部的0自動顯示
恢復舊格式:
取消設置:
方法1:保存舊設置,然后重置設置
方法2:unsetf()
? 函數原型為:
void unsetf(fmtflags mask);//mask是一個位,如果setf()將mask位設置為1,則unsetf()將mask位設置為0舉例:
cout << "ios_base成員函數setf()的使用***********************************************************"<< endl; cout << "一個參數的setf():********************************" << endl; int temperature = 63; cout << "Today's water temperature: "; cout.setf(ios_base::showpos); // show plus sign cout << temperature << endl; cout << "For our programming friends, that's\n"; cout << std::hex << temperature << endl; // use hex cout.setf(ios_base::uppercase); // use uppercase in hex cout.setf(ios_base::showbase); // use 0X prefix for hex cout << "or\n"; cout << temperature << endl; cout << "How " << true << "! oops -- How "; cout.setf(ios_base::boolalpha); cout << true << "!\n"; cout << "兩個參數的setf():*********************************" << endl; cout << dec; // use left justification, show the plus sign, show trailing // zeros, with a precision of 3 cout.fill(' '); cout.setf(ios_base::left, ios_base::adjustfield); cout.setf(ios_base::showpos); cout.setf(ios_base::showpoint); cout.precision(3); // use e-notation and save old format setting ios_base::fmtflags old = cout.setf(ios_base::scientific,ios_base::floatfield); cout << "Left Justification:\n"; long n; for (n = 1; n <= 41; n += 10) {cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n"; } // change to internal justification cout.setf(ios_base::internal, ios_base::adjustfield); // restore default floating-point display style cout.setf(old, ios_base::floatfield); cout << "Internal Justification:\n"; for (n = 1; n <= 41; n += 10) {cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n"; } // use right justification, fixed notation cout.setf(ios_base::right, ios_base::adjustfield); cout.setf(ios_base::fixed, ios_base::floatfield); cout << "Right Justification:\n"; for (n = 1; n <= 41; n += 10) {cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n"; } cout << "unsetf():*********************************" << endl; cout << boolalpha; cout << true << endl; cout.unsetf(ios_base::boolalpha); // display 1, 0 cout << true << endl; //將小數顯示模式設置為默認狀態,默認狀態即為混合模式和科學計數法模式都未設置 cout.setf(0, ios_base::floatfield); // go to default mode運行結果:
ios_base成員函數setf()的使用*********************************************************** 一個參數的setf():******************************** Today's water temperature: +63 For our programming friends, that's 3f or 0X3F How true! oops -- How true! 兩個參數的setf():********************************* Left Justification: +1 |+1.000E+00 | +11 |+3.317E+00 | +21 |+4.583E+00 | +31 |+5.568E+00 | +41 |+6.403E+00 | Internal Justification: + 1|+ 1.00| + 11|+ 3.32| + 21|+ 4.58| + 31|+ 5.57| + 41|+ 6.40| Right Justification:+1| +1.000|+11| +3.317|+21| +4.583|+31| +5.568|+41| +6.403| unsetf():********************************* true +12.13 標準流操縱符
實質上就是各種函數,可以直接使用cout<<流操縱符;即可設置相應的位置。
| boolalpha | setf(ios_base::boolalpha) |
| noboolalpha | unset(ios_base::noboolalpha) |
| showbase | setf(ios_base::showbase) |
| noshowbase | unsetf(ios_base::showbase) |
| showpoint | setf(ios_base::showpoint) |
| noshowpoint | unsetf(ios_base::showpoint) |
| showpos | setf(ios_base::showpos) |
| noshowpos | unsetf(ios_base::showpos) |
| uppercase | setf(ios_base::uppercase) |
| nouppercase | unsetf(ios_base::uppercase) |
| internal | setf(ios_base::internal,ios_base::adjustfield) |
| left | setf(ios_base::left,ios_base::adjustfield) |
| right | setf(ios_base::right,ios_base::adjustfield) |
| dec | setf(ios_base::dec, ios_base::basefield) |
| hex | setf(ios_base::hex, ios_base::basefield) |
| oct | setf(ios_base::oct, ios_base::basefield) |
| fixed | setf(ios_base::fixed,ios_base::floatfield) |
| scientific | setf(ios_base::scientific,ios_base::floatfield) |
舉例:
cout<<otc;2.14 iomanip頭文件
iomanip頭文件為precision() fill() width()準備的流操縱符(更方便使用)
ostream & setprecision(int);//設置精度 ostream & setfill(char);//設置填充符 ostream & setw(int);//設置寬度 //返回值使得可以實現輸出連接舉例:
cout << "iomanip頭文件************************************************************************" << endl; cout << noshowpos; // 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; }運行結果:
iomanip頭文件************************************************************************N square root fourth root ....10 3.162 1.7783 ....20 4.472 2.1147 ....30 5.477 2.3403 ....40 6.325 2.5149 ....50 7.071 2.6591 ....60 7.746 2.7832 ....70 8.367 2.8925 ....80 8.944 2.9907 ....90 9.487 3.0801 ...100 10.000 3.16233 完整測試代碼
/* Project name : _1cout_usage Last modified Date: 2022年3月12日11點52分 Last Version: V1.0 Descriptions: cout的常用用法 */ #include <iostream> #include <iomanip> #include <cmath>int main() {using namespace std;//將命名空間std的所有名字都引用了cout << "輸出與指針*********************************************************************" << endl;int eggs = 12;const char* amount = "dozen";cout << &eggs; // prints address of eggs variablecout << amount; // prints the string "dozen"cout << (void*)amount; // prints the address of the "dozen" stringcout << "輸出連接***********************************************************************" << endl;cout << "We have " << eggs << " unhatched chickens.\n";cout << "cout.put()*********************************************************************" << endl;cout.put('W'); // display the W charactercout.put('I').put('t'); // displaying It with two put() callscout.put(65); // display the A charactercout.put(66.3); // display the B charactercout.put(65); // display the A charactercout.put(66.3) << endl; // display the B charactercout << "cout.write()*******************************************************************" << endl;const char* state1 = "Florida";const char* state2 = "Kansas";const char* state3 = "Euphoria";int len = std::strlen(state2);cout << "Increasing loop index:\n";int i;for (i = 1; i <= len; i++){cout.write(state2, i);cout << endl;}// concatenate outputcout << "Decreasing loop index:\n";for (i = len; i > 0; i--)cout.write(state2, i) << endl;// exceed string lengthcout << "Exceeding string length:\n";cout.write(state2, len + 5) << endl;long val = 560031841;cout.write((char*)&val, sizeof(long))<<endl;cout << "清空輸出緩存*******************************************************************" << endl;cout << "Enter a number: ";float num;cin >> num;cout << "Hello, good-looking! " << flush;cout << "Wait just a moment, please." << endl;cout << "cout針對不同數據類型的輸出格式***************************************************" << endl;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";cout << "cout以不同進制顯示整數***********************************************************" << endl;/*cout以不同進制顯示整數*/i = 90;cout << "以十進制顯示i:" << dec << i << endl;//以十進制顯示cout << "以八進制顯示i:" << oct << i << endl;//以八進制顯示cout << "以16進制顯示i:" << hex << i << endl;//以16進制顯示//如需更改為十進制顯示方式,則可以使用以下方式cout.setf(ios_base::dec);cout << "以十進制顯示i:" << i << endl << endl;/*bool數據類型顯示*/cout << "bool數據類型顯示" << endl;bool is_true = true;cout.setf(ios_base::boolalpha);//可以顯示為true或falsecout << "is_true = " << is_true << endl;is_true = false;cout << "is_true = " << is_true << endl << endl;cout << "width()***************************************************************************" << endl;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";}cout.width(5);cout << 9.888889999 << endl;//將不會截斷cout << "更改填充字符****************************************************************************" << endl;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";}cout << "設置小數顯示精度:precision()***********************************************************" << endl;double j = 3333.1415926;/*設置顯示有效數字位數*/cout << "默認情況顯示(6位):" << j << endl;//輸出浮點數時,默認情況下保留六位有效數字cout.precision(9);//可以使用cout.precision(n)設置輸出浮點數時保留n位有效數字cout << "設置有效數字位數為9位時:" << j << endl;cout.precision(3);//當有效數字位數小于整數有效數字位數時,使用科學計數法顯示cout << "設置有效數字位數為3位時:" << j << endl << endl;cout << "ios_base成員函數setf()的使用***********************************************************"<< endl;cout << "一個參數的setf():********************************" << endl;int temperature = 63;cout << "Today's water temperature: ";cout.setf(ios_base::showpos); // show plus signcout << temperature << endl;cout << "For our programming friends, that's\n";cout << std::hex << temperature << endl; // use hexcout.setf(ios_base::uppercase); // use uppercase in hexcout.setf(ios_base::showbase); // use 0X prefix for hexcout << "or\n";cout << temperature << endl;cout << "How " << true << "! oops -- How ";cout.setf(ios_base::boolalpha);cout << true << "!\n";cout << "兩個參數的setf():*********************************" << endl;cout << dec;// use left justification, show the plus sign, show trailing// zeros, with a precision of 3cout.fill(' ');cout.setf(ios_base::left, ios_base::adjustfield);cout.setf(ios_base::showpos);cout.setf(ios_base::showpoint);cout.precision(3);// use e-notation and save old format settingios_base::fmtflags old = cout.setf(ios_base::scientific,ios_base::floatfield);cout << "Left Justification:\n";long n;for (n = 1; n <= 41; n += 10){cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n";}// change to internal justificationcout.setf(ios_base::internal, ios_base::adjustfield);// restore default floating-point display stylecout.setf(old, ios_base::floatfield);cout << "Internal Justification:\n";for (n = 1; n <= 41; n += 10){cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n";}// use right justification, fixed notationcout.setf(ios_base::right, ios_base::adjustfield);cout.setf(ios_base::fixed, ios_base::floatfield);cout << "Right Justification:\n";for (n = 1; n <= 41; n += 10){cout.width(4);cout << n << "|";cout.width(12);cout << sqrt(double(n)) << "|\n";}cout << "unsetf():*********************************" << endl;cout << boolalpha;cout << true << endl;cout.unsetf(ios_base::boolalpha); // display 1, 0cout << true << endl;//將小數顯示模式設置為默認狀態,默認狀態即為混合模式和科學計數法模式都未設置cout.setf(0, ios_base::floatfield); // go to default modecout << "iomanip頭文件************************************************************************" << endl;cout << noshowpos;// use new standard manipulatorscout << fixed << right;// use iomanip manipulatorscout << 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;}return 0; }總結
以上是生活随笔為你收集整理的C++ cout的使用,看这一篇就够了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kalilinux装到u盘上的弊端_付费
- 下一篇: UCI数据集+机器学习+十折交叉验证