📄 c++的iostream标准库介绍.htm
字号:
color=#0000ff>namespace</FONT>std; <BR><FONT
color=#0000ff>int</FONT><FONT color=#0000ff>main</FONT>()
<BR>{ <BR><FONT color=#0000ff>int</FONT>arraysize=1; <BR><FONT
color=#0000ff>char</FONT>*pbuffer=<FONT
color=#0000ff>new</FONT><FONT
color=#0000ff>char</FONT>[arraysize];
<BR>ostrstreamostr(pbuffer,arraysize,<FONT
color=#800000>ios</FONT>::out);
<BR>ostr<<arraysize<<ends;<FONT
color=#008000>//使用ostrstream输出到流对象的时候,要用ends结束字符串</FONT><BR><FONT
color=#800000>cout</FONT><<pbuffer; <BR><FONT
color=#0000ff>delete</FONT>[]pbuffer; <BR>system("pause");
<BR>}</TD></TR></TBODY></TABLE><BR> 上面的代码中,我们创建一个c风格的串流输出对象ostr,我们将arraysize内的数据成功的以字符串的形式输出到了ostr对象所指向的pbuffer指针的堆空间中,pbuffer也正是我们要输出的字符串数组,在结尾要使用<STRONG><FONT
color=#ff0000>ends</FONT></STRONG>结束字符串,如果不这么做就有溢出的危险。<BR></P>
<P>[!--empirenews.page--]C++的iostream标准库介绍(5)[/!--empirenews.page--]</P>接下来我们继续看一下<FONT
color=#ff0000><STRONG>C++风格的串流控制</STRONG></FONT>,C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。
<BR><BR> istringstream类用于执行C++风格的串流的输入操作。
<BR> stringstream类同时可以支持C++风格的串流的输入输出操作。
<BR> strstream类同时可以支持C风格的串流的输入输出操作。<BR><BR> istringstream类是从istream(输入流类)和stringstreambase(c++字符串流基类)派生而来,ostringstream是从ostream(输出流类)和stringstreambase(c++字符串流基类)派生而来,stringstream则是从iostream(输入输出流类)和和stringstreambase(c++字符串流基类)派生而来。
<BR><BR> 他们的继承关系如下图所示:<BR><BR><IMG
src="C++的iostream标准库介绍.files/3caa61f1acada65b4057ff7e07681ce8.gif"
onload="function anonymous() { var image=new Image();image.src=this.src;if(image.width>0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}} }"
border=0>
<BR><BR> istringstream是由一个string对象构造而来,istringstream类从一个string对象读取字符。
<BR> istringstream的构造函数原形如下:
<BR> istringstream::istringstream(string str); <BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9
border=1>
<TBODY>
<TR>
<TD><FONT color=#008000>//程序作者:管宁</FONT> <BR><FONT
color=#008000>//站点:www.cndev-lab.com</FONT> <BR><FONT
color=#008000>//所有稿件均有版权,如要转载,请务必著名出处和作者</FONT>
<BR>#include<<FONT color=#800000>iostream</FONT>>
<BR>#include<<FONT color=#800000>sstream</FONT>>
<BR><FONT color=#0000ff>using</FONT><FONT
color=#0000ff>namespace</FONT>std; <BR><FONT
color=#0000ff>int</FONT><FONT color=#0000ff>main</FONT>()
<BR>{ <BR>istringstreamistr; <BR>istr.str("156.7",); <BR><FONT
color=#008000>//上述两个过程可以简单写成istringstreamistr("156.7");</FONT><BR><FONT
color=#800000>cout</FONT><<istr.str()<<endl;
<BR><FONT color=#0000ff>int</FONT>a; <BR><FONT
color=#0000ff>float</FONT>b; <BR>istr>>a; <BR><FONT
color=#800000>cout</FONT><<a<<endl;
<BR>istr>>b; <BR><FONT
color=#800000>cout</FONT><<b<<endl;
<BR>system("pause"); <BR>}
</TD></TR></TBODY></TABLE><BR> 上例中,构造字符串流的时候,空格会成为字符串参数的内部分界,例子中对a,b对象的输入"赋值"操作证明了这一点,字符串的空格成为了整型数据与浮点型数据的分解点,利用分界获取的方法我们事实上完成了字符串到整型对象与浮点型对象的拆分转换过程。
<BR><BR> str()成员函数的使用可以让istringstream对象返回一个string字符串(例如本例中的输出操作(cout<<istr.str();)。<BR><BR> ostringstream同样是由一个string对象构造而来,ostringstream类向一个string插入字符。
<BR> ostringstream的构造函数原形如下:
<BR> ostringstream::ostringstream(string str);
<BR><BR> 示例代码如下:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9
border=1>
<TBODY>
<TR>
<TD><FONT color=#008000>//程序作者:管宁</FONT> <BR><FONT
color=#008000>//站点:www.cndev-lab.com</FONT> <BR><FONT
color=#008000>//所有稿件均有版权,如要转载,请务必著名出处和作者</FONT>
<BR>#include<<FONT color=#800000>iostream</FONT>>
<BR>#include<<FONT color=#800000>sstream</FONT>>
<BR>#include<<FONT color=#800000>string</FONT>>
<BR><FONT color=#0000ff>using</FONT><FONT
color=#0000ff>namespace</FONT>std; <BR><FONT
color=#0000ff>int</FONT><FONT color=#0000ff>main</FONT>()
<BR>{ <BR>ostringstreamostr; <BR><FONT
color=#008000>//ostr.str("abc");//如果构造的时候设置了字符串参数,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长</FONT><BR>ostr.put('d');
<BR>ostr.put('e'); <BR>ostr<<"fg"; <BR><BR><FONT
color=#800000>string</FONT>gstr<FONT
color=#ff0000>=</FONT>ostr.str(); <BR><FONT
color=#800000>cout</FONT><<gstr; <BR>system("pause");
<BR>}</TD></TR></TBODY></TABLE><BR> 在上例代码中,我们通过put()或者左移操作符可以不断向ostr插入单个字符或者是字符串,通过str()函数返回增长过后的完整字符串数据,但值得注意的一点是,当构造的时候对象内已经存在字符串数据的时候,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长。<BR><BR> 对于stringstream了来说,不用我多说,大家也已经知道它是用于C++风格的字符串的输入输出的。
<BR><BR> stringstream的构造函数原形如下:
<BR><BR> stringstream::stringstream(string
str);<BR><BR> 示例代码如下:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9
border=1>
<TBODY>
<TR>
<TD><FONT color=#008000>//程序作者:管宁</FONT> <BR><FONT
color=#008000>//站点:www.cndev-lab.com</FONT> <BR><FONT
color=#008000>//所有稿件均有版权,如要转载,请务必著名出处和作者</FONT>
<BR>#include<<FONT color=#800000>iostream</FONT>>
<BR>#include<<FONT color=#800000>sstream</FONT>>
<BR>#include<<FONT color=#800000>string</FONT>>
<BR><FONT color=#0000ff>using</FONT><FONT
color=#0000ff>namespace</FONT>std; <BR><BR><FONT
color=#0000ff>int</FONT><FONT color=#0000ff>main</FONT>()
<BR>{ <BR>stringstreamostr("ccc"); <BR>ostr.put('d');
<BR>ostr.put('e'); <BR>ostr<<"fg"; <BR><FONT
color=#800000>string</FONT>gstr<FONT
color=#ff0000>=</FONT>ostr.str(); <BR><FONT
color=#800000>cout</FONT><<gstr<<endl;
<BR><BR><FONT color=#0000ff>char</FONT>a; <BR>ostr>>a;
<BR><FONT color=#800000>cout</FONT><<a
<BR><BR>system("pause");
<BR>}</TD></TR></TBODY></TABLE><BR> 除此而外,stringstream类的对象我们还常用它进行string与各种内置类型数据之间的转换。
<BR><BR> 示例代码如下:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9
border=1>
<TBODY>
<TR>
<TD><FONT color=#008000>//程序作者:管宁</FONT> <BR><FONT
color=#008000>//站点:www.cndev-lab.com</FONT> <BR><FONT
color=#008000>//所有稿件均有版权,如要转载,请务必著名出处和作者</FONT>
<BR>#include<<FONT color=#800000>iostream</FONT>>
<BR>#include<<FONT color=#800000>sstream</FONT>>
<BR>#include<<FONT color=#800000>string</FONT>>
<BR><FONT color=#0000ff>using</FONT><FONT
color=#0000ff>namespace</FONT>std; <BR><BR><FONT
color=#0000ff>int</FONT><FONT color=#0000ff>main</FONT>()
<BR>{ <BR>stringstreamsstr; <BR><FONT
color=#008000>//--------int转string-----------</FONT><BR><FONT
color=#0000ff>int</FONT>a=100; <BR><FONT
color=#800000>string</FONT>str; <BR>sstr<<a;
<BR>sstr>>str; <BR><FONT
color=#800000>cout</FONT><<str<<endl; <BR><FONT
color=#008000>//--------string转char[]--------</FONT><BR>sstr.clear();<FONT
color=#008000>//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。</FONT><BR><FONT
color=#800000>string</FONT>name<FONT
color=#ff0000>=</FONT>"colinguan"; <BR><FONT
color=#0000ff>char</FONT>cname[200]; <BR>sstr<<name;
<BR>sstr>>cname; <BR><FONT
color=#800000>cout</FONT><<cname; <BR>system("pause");
<BR>}</TD></TR></TBODY></TABLE><BR>
<P>
<P>[!--empirenews.page--]C++的iostream标准库介绍(6)[/!--empirenews.page--]</P>接下来我们来学习一下<STRONG><FONT
color=#ff0000>输入/输出的状态标志</FONT></STRONG>的相关知识,C++中负责的输入/输出的系统包括了关于每一个输入/输出操作的结果的记录信息。这些当前的状态信息被包含在io_state类型的对象中。io_state是一个枚举类型(就像open_mode一样),以下便是它包含的值。
<BR><BR> goodbit 无错误 <BR><BR> Eofbit 已到达文件尾 <BR><BR> failbit
非致命的输入/输出错误,可挽回 <BR><BR> badbit 致命的输入/输出错误,无法挽回
<BR><BR> 有两种方法可以获得输入/输出的状态信息。一种方法是通过调用rdstate()函数,它将返回当前状态的错误标记。例如,假如没有任何错误,则rdstate()会返回goodbit.
<BR><BR> 下例示例,表示出了rdstate()的用法: <BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9
border=1>
<TBODY>
<TR>
<TD><FONT color=#008000>//程序作者:管宁</FONT> <BR><FONT
color=#008000>//站点:www.cndev-lab.com</FONT> <BR><FONT
color=#008000>//所有稿件均有版权,如要转载,请务必著名出处和作者</FONT>
<BR><BR>#include<<FONT color=#800000>iostream</FONT>>
<BR><FONT color=#0000ff>using</FONT><FONT
color=#0000ff>namespace</FONT>std; <BR><BR><FONT
color=#0000ff>int</FONT><FONT color=#0000ff>main</FONT>()
<BR>{ <BR><FONT color=#0000ff>int</FONT>a; <BR><FONT
color=#800000>cin</FONT>>>a; <BR><FONT
color=#800000>cout</FONT><<<FONT
color=#800000>cin</FONT>.rdstate()<<endl; <BR><FONT
color=#0000ff>if</FONT>(<FONT
color=#800000>cin</FONT>.rdstate()==<FONT
color=#800000>ios</FONT>::goodbit) <BR>{ <BR><FONT
color=#800000>cout</FONT><<"输入数据的类型正确,无错误!"<<endl;
<BR>} <BR><FONT color=#0000ff>if</FONT>(<FONT
color=#800000>cin</FONT>.rdstate()==ios_base::failbit) <BR>{
<BR><FONT
color=#800000>cout</FONT><<"输入数据类型错误,非致命错误,可清除输入缓冲区挽回!"<<endl;
<BR>} <BR>system("pause");
<BR>}</TD></TR></TBODY></TABLE><BR> 另一种方法则是使用下面任何一个函数来检测相应的输入/输出状态:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9
border=1>
<TBODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -