• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 200, comments - 8, trackbacks - 0, articles - 0

            c++ cout 格式化輸出(轉)

            Posted on 2012-11-19 14:42 鑫龍 閱讀(541) 評論(0)  編輯 收藏 引用 所屬分類: c++

            cout 格式化輸出


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

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


            將 bool 值以 literals 輸出

            cout <<"numeric : " <<true <<" or " <<false <<endl ; // 1 or 0 cout <<"literals : " <<boolalpha <<true <<" or " <<false <<endl ; // true or false cout <<"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 值的輸出將恢復 numeric 格式


            指定 Integral Values 的 Base

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


            顯示表明 Integer Values 的 Base

            cout <<showbase ; // Show base when printing integral values cout <<"oct : " <<oct <<ival <<endl ; // 21 : 8 進制 cout <<"dec : " <<dec <<ival <<endl ; // 017 : 10 進制 cout <<"hex : " <<hex <<ival <<endl ; // 0x11 : 16 進制 cout <<"hex : " <<hex <<17.01 <<endl ; // 17.01 : 不受影響 cout <<noshowbase ; // Reset state of the stream

            若想改變16進制字母的大小, 可以結合 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 6 pricision
               控制了至多一共會輸出多少個數字.  
               當要輸出的數字多余指定的值時, 將發生 四舍五入(rounded);  
               當要輸出的數字少于指定的值時, 則實際輸出的數字個數將少于指定值.
            // cout.pricision(4) ; // 等價于 cout <<setprecision(4) ; cout <<setprecision(4) <<12.345678 <<endl ; // 12.35 rounded! cout <<setprecision(10) <<12.345678 <<endl ; // 12.345678 其實內部發生了 rounded, 而結果正好進位, 與原值相同 cout <<cout.precision() <<endl ; // 輸出當前精度
             二: 表現形式 notation : 'very large and very small values are printed using scientific notation. other values use fixed decimal.'
               notation 控制了輸出的形式 : 科學計數法(scientific) 和 定點小數(fixed)
            float f = 101 / 6.0 ; cout <<fixed <<f <<endl ; // 16.83334 : 小數點后共6位 cout <<scientific <<f <<endl ; // 1.683333e+001 : 小數點后共6位
              恢復到初始狀態
            cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notation cout <<f <<endl ; // 16.8333 : 所有數字共6位
             三: 輸出十進制浮點 '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 ; // 10 cout <<showpoint <<10.0 <<endl ; // 10.0000 cout <<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.3 cout <<setw(10) <<12 <<3 <<endl ; // ________123 cout <<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 default cout <<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 default cout <<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 default cout <<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' characters setbase(int b) Output integers in base 'b'(only 'b' is 8/10/16 could the function work)
            欧美精品福利视频一区二区三区久久久精品| 激情五月综合综合久久69| 亚洲国产精品无码久久久不卡 | 久久成人18免费网站| 久久亚洲AV无码西西人体| 中文字幕久久波多野结衣av| 精品久久无码中文字幕| 三级韩国一区久久二区综合| 久久天天躁狠狠躁夜夜avapp| 国产精品久久久久乳精品爆| 国产69精品久久久久APP下载 | 国产精品免费看久久久| 久久久久久毛片免费看| 久久国产精品无码一区二区三区| 久久精品国产第一区二区| jizzjizz国产精品久久| 久久伊人精品一区二区三区| 国产99久久久国产精免费| 久久w5ww成w人免费| 久久狠狠爱亚洲综合影院 | 精品久久久久久无码专区| 久久久久久精品久久久久| 久久久久人妻精品一区三寸蜜桃 | 久久无码国产专区精品| 欧美伊香蕉久久综合类网站| 日日噜噜夜夜狠狠久久丁香五月| 日韩欧美亚洲国产精品字幕久久久| 久久久久久久99精品免费观看| 欧美熟妇另类久久久久久不卡| 亚洲国产精品成人AV无码久久综合影院| 久久99精品综合国产首页| 国产产无码乱码精品久久鸭| 色婷婷综合久久久久中文一区二区 | 久久九九兔免费精品6| 国产精品成人久久久| 亚洲精品无码久久久| 一级做a爰片久久毛片看看| 久久久高清免费视频| 色综合久久中文字幕综合网| 久久青青草原精品国产不卡| 欧美久久久久久午夜精品|