IO cout

1>重载<<操作符
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/*1*/ostream & operator<< (...)    

 

 

2>其他ostream方法
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/*1*/ostream & put(char)   //输出一个字符,返回一个ostream对象
//ep:
cout.put('a').put('65') << endl;

/*2*/basic_ostream<charT, traits>& write(const char_types *s, streamsize n)
//输出字符串,第一个参数说明要输出字符串地址,第二个参数指明输出字符个数并可内存越界
//ep:
cout.write(state2, len + 5).write(state2, len) << endl;

 

 

3>刷新输出缓冲区
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
/*1*/flush(cout)
//ep:
cout << flush;

/*2*/endl
//ep:
cout << endl;

 

 

4>用cout进行格式化
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/*1*/调整字段宽度
int width();             //返回当前字符宽度设置
int width(int i);    //将字符宽度设为i 返回以前字符宽度
//  以上符号影响且只影响接下来的第一个输出
//ep:
cout.width(5);

/*2*/填充字符
//ep:
cout.fill('*');

/*3*/设置浮点显示精度
//ep:
cout.precision(5);   //默认情况显示总位数,定点和科学模式指的是小数点后

 

 

5>有关setf()设置
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
fmtflags setf(fmtflags);           //fmtflags是bitmask类型
fmtflags setf(fmtflags,fmtflags);

/*1*/(一个参数)
     ios_base::boolalpha    输入和输出bool值,可以为true或false
     ios_base::showbase     对于输出,使用c++基数前缀(0,0x)
     ios_base::showpoint    显示末尾得小数点
     ios_base::uppercase  对于16进制输出,使用大写字母,E表示法
     ios_base::showpos      在正数前面加上+

/*2*/(两个参数)
第2个参数        第1个参数            含义
ios_base::basefield   ios_base::dec           使用基数10
                      ios_base::oct         使用基数8
                      ios_base::hex         使用基数16
/**************************************************************/
ios_base::floatfield  ios_base::fixed         使用定点计数法
                      ios_base::scientific    使用科学计数法
/**************************************************************/
ios_base::adjustfield ios_base::left          使用左对齐
                      ios_base::right     使用右对齐
                      ios_base::internal    符号或基数前缀左对齐,值右对齐

 

 

5>有关unsetf()取消状态函数得使用
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//ep:
 cout.unsetf(ios_base::floatfield);

 


5>有关setf()设置状态保存
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//ep:
ios_base::fmtflags old = cout.setf(ios_base::left, ios_base::adjustfield);

cout.setf(old, ios_base::adjustfield);

 

 

6>#include <iomanip> 控制符
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
操纵符        调用
boolalpha         setf(ios_base::boolalpha)
noboolalpha       unsetf(ios_base::boolalpha)

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)
unshowpos         unsetf(ios_base::showpos)

uppercase         setf(ios_base::uppercase)
nouppercase       unsetf(ios_base::uppercase)

iternal           setf(ios_base::iternal, 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)

****************************************************************
setprecision()       setw()           setfill()

posted @ 2007-03-21 22:49  Edward Xie  阅读(436)  评论(0)    收藏  举报