C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹
C++的sstream標(biāo)準(zhǔn)庫介紹
接下來我們繼續(xù)看一下C++風(fēng)格的串流控制 ,C++引入了ostringstream、istringstream、stringstream這三個類,要使用他們創(chuàng)建對象就必須包含sstream.h頭文件。
istringstream類用于執(zhí)行C++風(fēng)格的串流的輸入操作。
ostringstream類用于執(zhí)行C風(fēng)格的串流的輸出操作。
strstream類同時可以支持C風(fēng)格的串流的輸入輸出操作。
istringstream類是從istream(輸入流類)和stringstreambase(c++字符串流基類)派生而來,ostringstream是從ostream(輸出流類)和stringstreambase(c++字符串流基類)派生而來,stringstream則是從iostream(輸入輸出流類)和和stringstreambase(c++字符串流基類)派生而來。
他們的繼承關(guān)系如下圖所示:
istringstream是由一個string對象構(gòu)造而來,istringstream類從一個string對象讀取字符。
istringstream的構(gòu)造函數(shù)原形如下:
istringstream::istringstream(string str);
#include <iostream >
#include <sstream >
using namespace std;
int main ()
{
istringstream istr;
istr.str("1 56.7",);
//上述兩個過程可以簡單寫成 istringstream istr("1 56.7");
cout << istr.str()<<endl;
int a;
float b;
istr>>a;
cout <<a<<endl;
istr>>b;
cout <<b<<endl;
system("pause");
}
上例中,構(gòu)造字符串流的時候,空格會成為字符串參數(shù)的內(nèi)部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點,字符串的空格成為了整型數(shù)據(jù)與浮點型數(shù)據(jù)的分解點,利用分界獲取的方法我們事實上完成了字符串到整型對象與浮點型對象的拆分轉(zhuǎn)換過程。
str()成員函數(shù)的使用可以讓istringstream對象返回一個string字符串(例如本例中的輸出操作(cout<<istr.str();)。
ostringstream同樣是由一個string對象構(gòu)造而來,ostringstream類向一個string插入字符。
ostringstream的構(gòu)造函數(shù)原形如下:
ostringstream::ostringstream(string str);
示例代碼如下:
#include <iostream >
#include <sstream >
#include <string >
using namespace std;
int main ()
{
ostringstream ostr;
//ostr.str("abc");//如果構(gòu)造的時候設(shè)置了字符串參數(shù),那么增長操作的時候不會從結(jié)尾開始增加,而是修改原有數(shù)據(jù),超出的部分增長
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout <<gstr;
system("pause");
}
在上例代碼中,我們通過put()或者左移操作符可以不斷向ostr插入單個字符或者是字符串,通過str()函數(shù)返回增長過后的完整字符串?dāng)?shù)據(jù),但值得注意的一點是,當(dāng)構(gòu)造的時候?qū)ο髢?nèi)已經(jīng)存在字符串?dāng)?shù)據(jù)的時候,那么增長操作的時候不會從結(jié)尾開始增加,而是修改原有數(shù)據(jù),超出的部分增長。
對于stringstream了來說,不用我多說,大家也已經(jīng)知道它是用于C++風(fēng)格的字符串的輸入輸出的。
stringstream的構(gòu)造函數(shù)原形如下:
stringstream::stringstream(string str);
示例代碼如下:
#include <iostream >
#include <sstream >
#include <string >
using namespace std;
int main ()
{
stringstream ostr("ccc");
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout <<gstr<<endl;
char a;
ostr>>a;
cout <<a
system("pause");
}
除此而外,stringstream類的對象我們還常用它進(jìn)行string與各種內(nèi)置類型數(shù)據(jù)之間的轉(zhuǎn)換。
示例代碼如下:
#include <iostream >
#include <sstream >
#include <string >
using namespace std;
int main ()
{
stringstream sstr;
//--------int轉(zhuǎn)string-----------
int a=100;
string str;
sstr<<a;
sstr>>str;
cout <<str<<endl;
//--------string轉(zhuǎn)char[]--------
sstr.clear();//如果你想通過使用同一stringstream對象實現(xiàn)多種類型的轉(zhuǎn)換,請注意在每一次轉(zhuǎn)換之后都必須調(diào)用clear()成員函數(shù)。
string name = "colinguan";
char cname[200];
sstr<<name;
sstr>>cname;
cout <<cname;
system("pause");
}
接下來我們來學(xué)習(xí)一下輸入/輸出的狀態(tài)標(biāo)志 的相關(guān)知識,C++中負(fù)責(zé)的輸入/輸出的系統(tǒng)包括了關(guān)于每一個輸入/輸出操作的結(jié)果的記錄信息。這些當(dāng)前的狀態(tài)信息被包含在io_state類型的對象中。io_state是一個枚舉類型(就像open_mode一樣),以下便是它包含的值。
goodbit 無錯誤
Eofbit 已到達(dá)文件尾
failbit 非致命的輸入/輸出錯誤,可挽回
badbit 致命的輸入/輸出錯誤,無法挽回
有兩種方法可以獲得輸入/輸出的狀態(tài)信息。一種方法是通過調(diào)用rdstate()函數(shù),它將返回當(dāng)前狀態(tài)的錯誤標(biāo)記。例如,假如沒有任何錯誤,則rdstate()會返回goodbit.
下例示例,表示出了rdstate()的用法:
#include <iostream >
using namespace std;
int main ()
{
int a;
cin >>a;
cout <<cin .rdstate()<<endl;
if (cin .rdstate() == ios ::goodbit)
{
cout <<"輸入數(shù)據(jù)的類型正確,無錯誤!"<<endl;
}
if (cin .rdstate() == ios_base::failbit)
{
cout <<"輸入數(shù)據(jù)類型錯誤,非致命錯誤,可清除輸入緩沖區(qū)挽回!"<<endl;
}
system("pause");
}
另一種方法則是使用下面任何一個函數(shù)來檢測相應(yīng)的輸入/輸出狀態(tài):
bool bad();
bool eof();
bool fail();
bool good();
下例示例,表示出了上面各成員函數(shù)的用法:
#include <iostream >
using namespace std;
int main ()
{
int a;
cin >>a;
cout <<cin .rdstate()<<endl;
if (cin .good())
{
cout <<"輸入數(shù)據(jù)的類型正確,無錯誤!"<<endl;
}
if (cin .fail())
{
cout <<"輸入數(shù)據(jù)類型錯誤,非致命錯誤,可清除輸入緩沖區(qū)挽回!"<<endl;
}
system("pause");
}
如果錯誤發(fā)生,那么流狀態(tài)既被標(biāo)記為錯誤,你必須清除這些錯誤狀態(tài),以使你的程序能正確適當(dāng)?shù)乩^續(xù)運行。要清除錯誤狀態(tài),需使用clear()函數(shù)。此函數(shù)帶一個參數(shù),它是你將要設(shè)為當(dāng)前狀態(tài)的標(biāo)志值。,只要將ios::goodbit作為實參。
示例代碼如下:
#include <iostream >
using namespace std;
int main ()
{
int a;
cin >>a;
cout <<cin .rdstate()<<endl;
cin .clear(ios ::goodbit);
cout <<cin .rdstate()<<endl;
system("pause");
}
通常當(dāng)我們發(fā)現(xiàn)輸入有錯又需要改正的時候,使用clear()更改標(biāo)記為正確后,同時也需要使用get()成員函數(shù)清除輸入緩沖區(qū),以達(dá)到重復(fù)輸入的目的。
示例代碼如下:
#include <iostream >
using namespace std;
int main ()
{
int a;
while (1)
{
cin >>a;
if (!cin )//條件可改寫為cin.fail()
{
cout <<"輸入有錯!請重新輸入"<<endl;
cin .clear();
cin .get();
}
else
{
cout <<a;
break ;
}
}
system("pause");
}
最后再給出一個對文件流錯誤標(biāo)記處理的例子,鞏固學(xué)習(xí),代碼如下:
#include <iostream >
#include <fstream >
using namespace std;
int main ()
{
ifstream myfile("c://1.txt",ios_base::in,0);
if (myfile.fail())
{
cout <<"文件讀取失敗或指定文件不存在!"<<endl;
}
else
{
char ch;
while (myfile.get(ch))
{
cout <<ch;
}
if (myfile.eof())
{
cout <<"文件內(nèi)容已經(jīng)全部讀完"<<endl;
}
while (myfile.get(ch))
{
cout <<ch;
}
}
system("pause");
}
欄 目:C語言
下一篇:深入解析C++中的構(gòu)造函數(shù)和析構(gòu)函數(shù)
本文標(biāo)題:C++的sstream標(biāo)準(zhǔn)庫詳細(xì)介紹
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4200.html
您可能感興趣的文章
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10c語言求1+2+...+n的解決方法
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解約瑟夫環(huán)的數(shù)學(xué)優(yōu)化方法
- 01-10深入二叉樹兩個結(jié)點的最低共同父結(jié)點的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計- 解析最少換車次數(shù)的問題詳解
- 01-10c語言 跳臺階問題的解決方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法