流类

流类的层次图:

 

流类

一、ios类

类ios是所有类的祖先,其中包括三种重要的特性:格式化标志、错误状态位、文件操作模式。

1.格式化标志

格式标记位的取值为0或1:0表示关闭(不使用此格式),1表示开启(使用此格式)。如下:

boolalpha 如开启,则输入和输出使用bool值(即Ture或False)
showbase 如开启,则对于输出,使用C++ 基数前缀(0,0x)
showpoint 如开启,则显示末尾的小数点
uppercase 如开启,则对于16进制,使用大写字母;对于10进制,使用E表示法
showpos 如开启,则在正数前面加上+

dec 如开启,则使用基数10(进行输出)
oct 如开启,则使用基数8 (进行输出)
hex 如开启,则使用基数16 (进行输出)
fixed 如开启,则使用定点计数法
scientific 如开启,则使用科学计数法
left 如开启,则使用左对齐
right 如开启,则使用右对齐
internal 如开启,则符号或基数前缀左对齐,值右对齐

skipws 如开启,则跳过输入流中的空白字符
unitbuf 如开启,则每次输出操作后都会清空缓冲区

1)以上所有格式化标志,都可以使用类ios的成员函数setf()和unsetf()来设置。

如:

cout.setf(ios::left); //做对齐
cout<<"输出的左对齐";
cout.unsetf(ios::left); //恢复默认

2)另外大部分格式化标志都可以用操作符来设置。

操作符:直接插入流中的格式化命令。操作符包括两类:带有参数和不带参数。

①不带参数的操作符:

ws           输入时跳过空格

dec         转化为十进制

oct          转化为八进制

hex         转化为十六进制

endl        在输出流中插入换行符并刷新它

ends        插入空字符来终止输出字符串

flush         刷新输出流

lock          锁定文件句柄

unlock      解锁文件句柄

可以把上面这些操作符直接插入流中。如  cout<<hex<<var;   //以十六进制输出var

②带有参数的操作符(概括了常用的,使用时需要包含头文件iomanip)

setw( )              字段款(int)           输出时设置字段款

setfill( )             填充字符(int)       输出时设置填充字符(默认空格)

setprecision( )   精度(int)            设置精度(显示多少位数字,用于显示浮点数的精度)

setiosflags( )      格式化标志(long) 设置指定标志

resetiosflags( )   格式化标志(long) 清除指定标志

MSDN上的实例:

// iomanip_setw.cpp 
// compile with: /EHsc
// Defines the entry point for the console application.
//
// Sample use of the following manipulators:
//   resetiosflags
//   setiosflags
//   setbase
//   setfill
//   setprecision
//   setw

#include <iostream>
#include <iomanip>

using namespace std;

const double   d1 = 1.23456789;
const double   d2 = 12.3456789;
const double   d3 = 123.456789;
const double   d4 = 1234.56789;
const double   d5 = 12345.6789;
const long      l1 = 16;
const long      l2 = 256;
const long      l3 = 1024;
const long      l4 = 4096;
const long      l5 = 65536;
int         base = 10;

void DisplayDefault( )
{
   cout << endl << "default display" << endl;
   cout << "d1 = " << d1 << endl;
   cout << "d2 = " << d2 << endl;
   cout << "d3 = " << d3 << endl;
   cout << "d4 = " << d4 << endl;
   cout << "d5 = " << d5 << endl;
}

void DisplayWidth( int n )
{
   cout << endl << "fixed width display set to " << n << ".n";
   cout << "d1 = " << setw(n) << d1 << endl;
   cout << "d2 = " << setw(n) << d2 << endl;
   cout << "d3 = " << setw(n) << d3 << endl;
   cout << "d4 = " << setw(n) << d4 << endl;
   cout << "d5 = " << setw(n) << d5 << endl;
}

void DisplayLongs( )
{
   cout << setbase(10);
   cout << endl << "setbase(" << base << ")" << endl;
   cout << setbase(base);
   cout << "l1 = " << l1 << endl;
   cout << "l2 = " << l2 << endl;
   cout << "l3 = " << l3 << endl;
   cout << "l4 = " << l4 << endl;
   cout << "l5 = " << l5 << endl;
}

int main( int argc, char* argv[] )
{
   DisplayDefault( );

   cout << endl << "setprecision(" << 3 << ")" << setprecision(3);
   DisplayDefault( );

   cout << endl << "setprecision(" << 12 << ")" << setprecision(12);
   DisplayDefault( );

   cout << setiosflags(ios_base::scientific);
   cout << endl << "setiosflags(" << ios_base::scientific << ")";
   DisplayDefault( );

   cout << resetiosflags(ios_base::scientific);
   cout << endl << "resetiosflags(" << ios_base::scientific << ")";
   DisplayDefault( );

   cout << endl << "setfill('" << 'S' << "')" << setfill('S');
   DisplayWidth(15);
   DisplayDefault( );

   cout << endl << "setfill('" << ' ' << "')" << setfill(' ');
   DisplayWidth(15);
   DisplayDefault( );

   cout << endl << "setprecision(" << 8 << ")" << setprecision(8);
   DisplayWidth(10);
   DisplayDefault( );

   base = 16;
   DisplayLongs( );

   base = 8;
   DisplayLongs( );

   base = 10;
   DisplayLongs( );

   return   0;
}

③ ios 的成员函数(可以用来设置格式化标志和执行其他任务)

ch=fill( )                   返回填充字符(填充未使用字段,默认为空格)

fill( ch )                     设置填充字段

p=precision( );        获得精度(显示浮点数小数点后的数字位数)

precision(p);            设置精度

w=width( )                获得当前字段宽度(以字符计)

width( w )                 设置当前字段宽度

setf(flags)                 设置指定的格式标志(例如:ios::left)

unself(flags)             清除指定的格式标志

setf(flags,fisld)         先清除字段,然后设置标志

函数setf()的两个参数的版本,使用第二个参数来重新设置特定的类型的所有标志,然后第一个参数中指定的标志被设置。

如:cout.setf(ios::left,ios::adjustfield)  //清除处理字段对齐的所有标志,然后设置输出左对齐

第一个参数:设置的标志 第二个参数:清除的字段
dec、oct、hex
left、right、internal
scientific、fixed
basefield
adjustfield
floatfield

 

二、istream类---由类ios派生而来,执行指定的输入操作或者析取

磁盘文件、键盘---->程序:

      intput、析取运算符(>>)、get( )、read( )

程序----->磁盘文件、屏幕:

      output、插入运算符(<<)、put( )、write( )

类istream最经常使用的函数:

函数 用途
>> 所有基本类型(和重载类型)的格式化输出
get(ch); 析取一个字符到ch中
get(str); 析取字符串到数组str中,直到遇到‘n’
get(str,MAX) 析取字符串到数组中,直到达到上限(MAX-1个元素)
get(str,DELIM) 析取字符串到str中,直到遇到分界符,分界符保留在流中
get(str,MAX,DELIM) 析取字符串到数组str中,直到遇到分界符DELIM或者达到字符上限,分界符保留在流中
getline(str,MAX,DELIM) 同上,析取分界符
putback(ch) 插入读入的最后字符到输出流中
ignore(MAX,DELIM) 析取并且丢弃最多MAX个字符,直到遇到指定的分界符
peek(ch) 读取一个字符,把他保留在流中(特性像get一样)
count=gcount( ) 返回调用get,getline,或者read所读到的字符数
read(str,MAX) 处理文件,析取最多MAX个字符到str,直到EOF
seekg( ) 设置从文件指定位置的指针偏移距离(字节数)
pos=tellg(pos) 返回从文件开始位置的指针偏移距离(字节数)

三、ostream类---由类ios派生而来,处理输出或者插入行为

函数 用途
<< 所有基本类型(和重载类型)的格式化插入
put(ch) 插入字符ch到流中
flush( ) 刷新换行符内容并且插入换行符
write(str,SIZE) 把数组str中SIZE个字符插入到文件中
seekg( ) 设置从文件指定位置的指针偏移距离(字节数)
pos=tellg(pos) 返回输出流中的指针偏移距离(字节数)

四、类iostream和带_withassign的类

1. 类iostream由类istream和类ostream派生而来,只作为其他类的基类,没有自己的函数(除了构造函数和析构函数)。

2. 3中带_withassign的类:

类istream_withassign由类istream派生

类ostream_withassign由类ostream派生

类iostream_withassign由类iostream派生

这些带_withassign的类同派生他们的基类类似,除了带有用于复制对象的重载运算符之外。

3. 预定义流对象

cin是类istream_withassign的对象,通常用于键盘输入

cout是类ostream_withassign的对象,通常用于屏幕输出

cerr是类ostream_withassign的对象,用于显示错误信息

clog是类ostream_withassign的对象,用于显示日志信息

注意:传递给cerr的输出将立即显示,而不是先被缓冲(那是cout 的做法)

原文链接: https://www.cnblogs.com/qlwy/archive/2011/11/09/2243661.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    流类

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/36151

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午12:55
下一篇 2023年2月8日 下午12:56

相关推荐