coutの出力フォーマットの設定方法(回転)

4286 ワード

coutの出力フォーマットの設定方法(回転)
プログラミングノートcoutの出力フォーマットには2つの方法があります.1つはcoutのメンバー変数で、1つは中で提供される関数です.
たとえば、出力の行幅を設定するには、次の2つの方法があります.

#include
#include
using namepsace std;
void main(){
     // using str::width()
     cout.width(20);
     cout<<12<
     // using iomanip
     cout<}

2つの方法は等価であり、実際にsetw(20)という関数はmanipulatorオブジェクトを返し、coutが属するostreamクラスはオペレータ<

if(manipulatorIn.type==is_seting_width){
     this->width( manipulator.width() );
}

2つ目の方法は書くのが簡単なので、ここでは2つ目の方法だけを紹介します.
6つのメンバー関数があり、それぞれ次のようになります.
resetiosflags   //Clears the specified flags. 
setbase         //Set base for integers. 
setfill             //Sets the character that will be used to fill spaces in a right-justified display. 
setiosflags      //Sets the specified flags. 
setprecision    //Sets the precision for floating-point values. 
setw             //Specifies the width of the display field. 
その中で最もよく使われるのはsetprecision,setw,setbaseです.
setprecisionは出力の精度を設定し、数桁の数字を表示します.それは後ろのcoutに影響を与えます.
setwは出力の行幅を設定し、行幅がprecisionの値より小さい場合はprecisionで表示し、行幅がprecisionの値より大きい場合は数字の前に塗りつぶす.setw()は、直後の数字にのみ有効です.
setbaseは出力数を設定する基数であり、8進数を出力する場合はsetbase(8)を用いる.setbaseの後ろのパラメータは8,10,16しかなく、他のパラメータは無効です.setbaseはその後のcoutに影響を及ぼす.
setfillは、入力に使用する文字を設定します.設定した行幅が出力の精度より大きい場合は、数値の前に塗りつぶし、塗りつぶしに使用する文字はsetfillで設定します.setfillはその後のcoutに影響を及ぼす.
setioflagsとresetioflagsは、いくつかのflag値を変更するために使用される一対の関数です.これらのflag値には、次のものがあります.
参照テキスト
boolalpha, to insert or extract objects of type bool as names (such as true and false) rather than as numeric values. 
dec, to insert or extract integer values in decimal format. 
fixed, to insert floating-point values in fixed-point format (with no exponent field). 
hex, to insert or extract integer values in hexadecimal format. 
internal, to pad to a field width as needed by inserting fill characters at a point internal to a generated numeric field. 
left, to pad to a field width as needed by inserting fill characters at the end of a generated field (left justification). 
oct, to insert or extract integer values in octal format. 
right, to pad to a field width as needed by inserting fill characters at the beginning of a generated field (right justification). 
scientific, to insert floating-point values in scientific format (with an exponent field). 
showbase, to insert a prefix that reveals the base of a generated integer field. 
showpoint, to insert a decimal point unconditionally in a generated floating-point field. 
showpos, to insert a plus sign in a nonnegative generated numeric field. 
skipws, to skip leading white space before certain extractions. 
unitbuf, to flush output after each insertion. 
uppercase, to insert uppercase equivalents of lowercase letters in certain insertions. 
In addition, several useful values are: 
adjustfield, where internal | left | right 
basefield, where dec | hex | oct 
floatfield, where fixed | scientific 
だから実際にこの2つの関数で多くのことをすることができますが、これらのflagを覚えるのは本当に面倒です.
例:

const double a=0.123456789;
const int b=16;
cout<< "a="<cout<< "setprecision(5)"<cout<< setprecision(5)<cout<< "setw(10)"<cout<< setw(10)<cout << "setiosflags(ios_base::scientific)"<cout<< setiosflags(ios_base::scientific)<cout<< "resetiosflags(ios_base::scientific)"<cout<< resetiosflags(ios_base::scientific)<cout<< "b="<cout<< "setbase(8)"<cout<

結果は次のとおりです.
a=0.123457
setprecision(5)
0.12346
setw(10)
    0.12346
setiosflags(ios_base::scientific)
1.23457e-001
resetiosflags(ios_base::scientific)
0.12346
b=16
setbase(8)
20