国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ cout格式化輸出

2019-11-08 20:17:04
字體:
供稿:網(wǎng)友

轉(zhuǎn)自:http://blog.csdn.net/dongfengkuayue/article/details/21229189

將 cout 的 flag 保存到變量, 以便修改后的恢復(fù)

    ostream::fmtflags old = cout.flag() ;        // 無參將返回當(dāng)前 flag 值    cout.flag(old) ;                            // 恢復(fù)到原先保存的值

將 bool 值以 literals 輸出

cout <<"numeric : " <<true <<" or " <<false <<endl ;              // 1 or 0cout <<"literals : " <<boolalpha <<true <<" or " <<false <<endl ; // true or falsecout <<"literals : " <<boolalpha <<0 <<endl ;                     // 0    原因: 0 在cout中不等價于 false

一旦我們使用 boolalpha 將改變 cout 對 bool 值的輸出格式. 此后的 cout 都會將 bool 輸出為 literals.

將 bool 值以 numeric 輸出

    cout <<"numeric : " <<noboolalpha <<true <<" or " <<false <<endl ;// 1 or 0

從此以后, cout 對 bool 值的輸出將恢復(fù) numeric 格式

指定 Integral Values 的 Base

    const int ival = 17 ;        // 'ival' is constant, so value never change    cout <<"oct : " <<oct <<ival <<endl ;        // 21 : 8 進(jìn)制    cout <<"dec : " <<dec <<ival <<endl ;        // 17 : 10 進(jìn)制    cout <<"hex : " <<hex <<ival <<endl ;        // 11 : 16 進(jìn)制    cout <<"hex : " <<hex <<17.01 <<endl ;        // 17.01 : 不受影響如 boolalpha 一樣, oct, dec, hex 也是 persistent. 一旦改變, 將影響后續(xù)的輸出格式.

顯示表明 Integer Values 的 Base

復(fù)制代碼
    cout <<showbase ;                            // Show base when PRinting integral values    cout <<"oct : " <<oct <<ival <<endl ;        // 21 : 8 進(jìn)制    cout <<"dec : " <<dec <<ival <<endl ;        // 017 : 10 進(jìn)制    cout <<"hex : " <<hex <<ival <<endl ;        // 0x11 : 16 進(jìn)制    cout <<"hex : " <<hex <<17.01 <<endl ;        // 17.01 : 不受影響    cout <<noshowbase ;                            // Reset state of the stream復(fù)制代碼

若想改變16進(jìn)制字母的大小, 可以結(jié)合 uppercase/nouppercase

    cout <<showbase <<uppercase ;    cout <<"hex : " <<hex <<15 <<endl ;            // 0XF 大寫形式    cout <<nouppercase ;    cout <<"hex : " <<hex <<15 <<endl ;            // 0xf 小寫形式

showbase 與 noshowbase 的作用周期也是 persistent

對于 float/double 型, 有三種格式化控制

一: 輸出精度 precision : by default is 6pricision   控制了至多一共會輸出多少個數(shù)字.     當(dāng)要輸出的數(shù)字多余指定的值時, 將發(fā)生 四舍五入(rounded);    當(dāng)要輸出的數(shù)字少于指定的值時, 則實(shí)際輸出的數(shù)字個數(shù)將少于指定值.
// cout.pricision(4) ;                         // 等價于 cout <<setprecision(4) ;cout <<setprecision(4) <<12.345678 <<endl ;    // 12.35  rounded!cout <<setprecision(10) <<12.345678 <<endl ;   // 12.345678 其實(shí)內(nèi)部發(fā)生了 rounded, 而結(jié)果正好進(jìn)位, 與原值相同cout <<cout.precision() <<endl ;               // 輸出當(dāng)前精度
 二: 表現(xiàn)形式 notation : 'very large and very small values are printed using scientific notation. other values use fixeddecimal.'   notation 控制了輸出的形式 : 科學(xué)計數(shù)法(scientific) 和 定點(diǎn)小數(shù)(fixed)
float f = 101 / 6.0 ;cout <<fixed <<f <<endl ;           // 16.83334 : 小數(shù)點(diǎn)后共6位cout <<scientific <<f <<endl ;      // 1.683333e+001 : 小數(shù)點(diǎn)后共6位  恢復(fù)到初始狀態(tài)
cout.unsetf(ostream::floatfield) ;  // Retrieve to default handling for notationcout <<f <<endl ;                   // 16.8333 : 所有數(shù)字共6位
 三: 輸出十進(jìn)制浮點(diǎn) 'By default, when the fractional part of a floating-point value is 0, the decimal point is not displayed. The showpoint manipulator forces the decimal point ot be printed.'
cout <<10.0 <<endl ;                // 10cout <<showpoint <<10.0 <<endl ;    // 10.0000cout <<noshowpoint <<endl ;         // Revert to default handling of decimal  

輸出填充 Padding the Output

   setw to specify the minimum space for the next numeric or string value.
cout <<setw(10) <<12.3 <<endl ;     // ______12.3cout <<setw(10) <<12 <<3 <<endl ;   // ________123cout <<setw(3) <<12.345 <<endl ;    // If the total output is more than 3, it can be extended 
   left to left-justify the output.
    cout <<left ;                                 // left-justify    cout <<setw(5) <<12 <<setw(5) <<34 <<endl ;   // 12___34___ 
   right to right-justify the output. Output is right-justified bu default.
cout <<right ;                                // By defaultcout <<setw(5) <<12 <<setw(5) <<34 <<endl ;   // 12___34___ 
   internal controls placement of the sign on negative value. internal left-justifies the sign and right-justifies the value, padding any intervening space with blanks.(if setfill not set) 
cout <<internal ;               // By defaultcout <<setw(5) <<-12 <<endl ;   // 12___34___ 
   setfill lets us specify an alternative character to use when padding the output. By default, the value is a space.
cout <<setfill('*') ;          // By defaultcout <<setw(5) <<12 <<endl ;   // 12___34___
 

Header Files

   Manipulators Defined in <iomanip>
setfill(char ch)        Fill whitespace with 'ch'setprecision(int n)     Set floating-point precision to 'n'setw(int w)             Read or write value to 'w' characterssetbase(int b)          Output integers in base 'b'(only 'b' is 8/10/16 could the function work) 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 融水| 镇康县| 西和县| 密云县| 宜兴市| 正阳县| 台中市| 孝感市| 平安县| 娱乐| 平乡县| 桐柏县| 调兵山市| 汕头市| 彰化市| 八宿县| 旬邑县| 顺平县| 郓城县| 和顺县| 江口县| 忻城县| 特克斯县| 灵宝市| 泉州市| 桃江县| 德昌县| 镇康县| 蓬莱市| 崇阳县| 潮州市| 毕节市| 永新县| 临漳县| 兰州市| 林甸县| 洪雅县| 永昌县| 沿河| 蓬安县| 合作市|