接下來我們繼續(xù)看一下C++風(fēng)格的串流控制,C++引入了ostringstream、istringstream、stringstream這三個(gè)類,要使用他們創(chuàng)建對象就必須包含sstream.h頭文件。
istringstream類用于執(zhí)行C++風(fēng)格的串流的輸入操作。
ostringstream類用于執(zhí)行C++風(fēng)格的串流的輸出操作。
stringstream類同時(shí)可以支持C++風(fēng)格的串流的輸入輸出操作。
istringstream類是從istream(輸入流類)和stringstreambase(c++字符串流基類)派生而來,ostringstream是從ostream(輸出流類)和stringstreambase(c++字符串流基類)派生而來,stringstream則是從iostream(輸入輸出流類)和和stringstreambase(c++字符串流基類)派生而來。
istringstream是由一個(gè)string對象構(gòu)造而來,istringstream類從一個(gè)string對象讀取字符。
istringstream的構(gòu)造函數(shù)原形如下:
istringstream::istringstream(string str);
示例代碼如下:
C++ 代碼 //程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
using namespace std;
int main()
{
istringstream istr;
istr.str("1 56.7",);
//上述兩個(gè)過程可以簡單寫成 istringstream istr("1 56.7");
cout << istr.str() float b;
istr>>a;
cout>b;
cout }
上例中,構(gòu)造字符串流的時(shí)候,空格會(huì)成為字符串參數(shù)的內(nèi)部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點(diǎn),字符串的空格成為了整型數(shù)據(jù)與浮點(diǎn)型數(shù)據(jù)的分解點(diǎn),利用分界獲取的方法我們事實(shí)上完成了字符串到整型對象與浮點(diǎn)型對象的拆分轉(zhuǎn)換過程。
str()成員函數(shù)的使用可以讓istringstream對象返回一個(gè)string字符串(例如本例中的輸出操作(cout< ostringstream同樣是由一個(gè)string對象構(gòu)造而來,ostringstream類向一個(gè)string插入字符。
ostringstream的構(gòu)造函數(shù)原形如下:
ostringstream::ostringstream(string str);
示例代碼如下:
C++ 代碼 //程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
#include
using namespace std;
int main()
{
ostringstream ostr;
//ostr.str("abc");//如果構(gòu)造的時(shí)候設(shè)置了字符串參數(shù),那么增長操作的時(shí)候不會(huì)從結(jié)尾開始增加,而是修改原有數(shù)據(jù),超出的部分增長
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout }
在上例代碼中,我們通過put()或者左移操作符可以不斷向ostr插入單個(gè)字符或者是字符串,通過str()函數(shù)返回增長過后的完整字符串?dāng)?shù)據(jù),但值得注意的一點(diǎn)是,當(dāng)構(gòu)造的時(shí)候?qū)ο髢?nèi)已經(jīng)存在字符串?dāng)?shù)據(jù)的時(shí)候,那么增長操作的時(shí)候不會(huì)從結(jié)尾開始增加,而是修改原有數(shù)據(jù),超出的部分增長。
對于stringstream了來說,不用我多說,大家也已經(jīng)知道它是用于C++風(fēng)格的字符串的輸入輸出的。
stringstream的構(gòu)造函數(shù)原形如下:
stringstream::stringstream(string str);
C++ 代碼 示例代碼如下:
//程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
#include
using namespace std;
int main()
{
stringstream ostr("ccc");
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout<
char a;
ostr>>a;
cout<
system("pause");
}
除此而外,stringstream類的對象我們還常用它進(jìn)行string與各種內(nèi)置類型數(shù)據(jù)之間的轉(zhuǎn)換。
示例代碼如下:
C++ 代碼 //程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
#include
using namespace std;
int main()
{
stringstream sstr;
//--------int轉(zhuǎn)string-----------
int a=100;
string str;
sstr< sstr>>str;
cout/--------string轉(zhuǎn)char[]--------
sstr.clear();//如果你想通過使用同一stringstream對象實(shí)現(xiàn)多種類型的轉(zhuǎn)換,請注意在每一次轉(zhuǎn)換之后都必須調(diào)用clear()成員函數(shù)。
string name = "colinguan";
char cname[200];
sstr< sstr>>cname;
cout< system("pause");
}
istringstream類用于執(zhí)行C++風(fēng)格的串流的輸入操作。
ostringstream類用于執(zhí)行C++風(fēng)格的串流的輸出操作。
stringstream類同時(shí)可以支持C++風(fēng)格的串流的輸入輸出操作。
istringstream類是從istream(輸入流類)和stringstreambase(c++字符串流基類)派生而來,ostringstream是從ostream(輸出流類)和stringstreambase(c++字符串流基類)派生而來,stringstream則是從iostream(輸入輸出流類)和和stringstreambase(c++字符串流基類)派生而來。
istringstream是由一個(gè)string對象構(gòu)造而來,istringstream類從一個(gè)string對象讀取字符。
istringstream的構(gòu)造函數(shù)原形如下:
istringstream::istringstream(string str);
示例代碼如下:
C++ 代碼 //程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
using namespace std;
int main()
{
istringstream istr;
istr.str("1 56.7",);
//上述兩個(gè)過程可以簡單寫成 istringstream istr("1 56.7");
cout << istr.str()
istr>>a;
cout
cout
上例中,構(gòu)造字符串流的時(shí)候,空格會(huì)成為字符串參數(shù)的內(nèi)部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點(diǎn),字符串的空格成為了整型數(shù)據(jù)與浮點(diǎn)型數(shù)據(jù)的分解點(diǎn),利用分界獲取的方法我們事實(shí)上完成了字符串到整型對象與浮點(diǎn)型對象的拆分轉(zhuǎn)換過程。
str()成員函數(shù)的使用可以讓istringstream對象返回一個(gè)string字符串(例如本例中的輸出操作(cout< ostringstream同樣是由一個(gè)string對象構(gòu)造而來,ostringstream類向一個(gè)string插入字符。
ostringstream的構(gòu)造函數(shù)原形如下:
ostringstream::ostringstream(string str);
示例代碼如下:
C++ 代碼 //程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
#include
using namespace std;
int main()
{
ostringstream ostr;
//ostr.str("abc");//如果構(gòu)造的時(shí)候設(shè)置了字符串參數(shù),那么增長操作的時(shí)候不會(huì)從結(jié)尾開始增加,而是修改原有數(shù)據(jù),超出的部分增長
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout
在上例代碼中,我們通過put()或者左移操作符可以不斷向ostr插入單個(gè)字符或者是字符串,通過str()函數(shù)返回增長過后的完整字符串?dāng)?shù)據(jù),但值得注意的一點(diǎn)是,當(dāng)構(gòu)造的時(shí)候?qū)ο髢?nèi)已經(jīng)存在字符串?dāng)?shù)據(jù)的時(shí)候,那么增長操作的時(shí)候不會(huì)從結(jié)尾開始增加,而是修改原有數(shù)據(jù),超出的部分增長。
對于stringstream了來說,不用我多說,大家也已經(jīng)知道它是用于C++風(fēng)格的字符串的輸入輸出的。
stringstream的構(gòu)造函數(shù)原形如下:
stringstream::stringstream(string str);
C++ 代碼 示例代碼如下:
//程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
#include
using namespace std;
int main()
{
stringstream ostr("ccc");
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout<
char a;
ostr>>a;
cout<
system("pause");
}
除此而外,stringstream類的對象我們還常用它進(jìn)行string與各種內(nèi)置類型數(shù)據(jù)之間的轉(zhuǎn)換。
示例代碼如下:
C++ 代碼 //程序作者:管寧
//站點(diǎn):www.cndev-lab.com
//所有稿件均有版權(quán),如要轉(zhuǎn)載,請務(wù)必出處和作者
#include
#include
#include
using namespace std;
int main()
{
stringstream sstr;
//--------int轉(zhuǎn)string-----------
int a=100;
string str;
sstr< sstr>>str;
cout/--------string轉(zhuǎn)char[]--------
sstr.clear();//如果你想通過使用同一stringstream對象實(shí)現(xiàn)多種類型的轉(zhuǎn)換,請注意在每一次轉(zhuǎn)換之后都必須調(diào)用clear()成員函數(shù)。
string name = "colinguan";
char cname[200];
sstr< sstr>>cname;
cout< system("pause");
}