0、C++的輸入輸出分為三種:
(1)基于控制臺(tái)的I/O

(2)基于文件的I/O

(3)基于字符串的I/O
1、頭文件
[cpp] view plaincopyprint?
2、作用
istringstream類用于執(zhí)行C++風(fēng)格的字符串流的輸入操作。
ostringstream類用于執(zhí)行C++風(fēng)格的字符串流的輸出操作。
strstream類同時(shí)可以支持C++風(fēng)格的串流的輸入輸出操作。
3、具體分析
istringstream類
描述:從流中提取數(shù)據(jù),支持 >> 操作
這里字符串可以包括多個(gè)單詞,單詞之間使用空格分開
- istringstream的構(gòu)造函數(shù)原形:
- istringstream::istringstream(string str);
初始化:使用字符串進(jìn)行初始化
- istringstream istr("1 56.7");
- istr.str("1 56.7");//把字符串"1 56.7"存入字符串流中
使用:我們可以使用分解點(diǎn)獲取不同的數(shù)據(jù),完成 字符串 到 其他類型 的轉(zhuǎn)換
常用成員函數(shù):
- str():使istringstream對(duì)象返回一個(gè)string字符串
舉例:把字符串類型的數(shù)據(jù)轉(zhuǎn)換為其他類型
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- istringstream istr("1 56.7");
-
- cout<<istr.str()<<endl;//直接輸出字符串的數(shù)據(jù) "1 56.7"
-
- string str = istr.str();//函數(shù)str()返回一個(gè)字符串
- cout<<str<<endl;
-
- int n;
- double d;
-
- //以空格為界,把istringstream中數(shù)據(jù)取出,應(yīng)進(jìn)行類型轉(zhuǎn)換
- istr>>n;//第一個(gè)數(shù)為整型數(shù)據(jù),輸出1
- istr>>d;//第二個(gè)數(shù)位浮點(diǎn)數(shù),輸出56.7
-
- //假設(shè)換下存儲(chǔ)類型
- istr>>d;//istringstream第一個(gè)數(shù)要自動(dòng)變成浮點(diǎn)型,輸出仍為1
- istr>>n;//istringstream第二個(gè)數(shù)要自動(dòng)變成整型,有數(shù)字的階段,輸出為56
-
- //測試輸出
- cout<<d<<endl;
- cout<<n<<endl;
- system("pause");
- return 1;
- }
舉例2:把一行字符串放入流中,單詞以空格隔開。之后把一個(gè)個(gè)單詞從流中依次讀取到字符串
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- istringstream istr;
- string line,str;
- while (getline(cin,line))//從終端接收一行字符串,并放入字符串line中
- {
- istr.str(line);//把line中的字符串存入字符串流中
- while(istr >> str)//每次讀取一個(gè)單詞(以空格為界),存入str中
- {
- cout<<str<<endl;
- }
- }
- system("pause");
- return 1;
- }
輸入:123 34 45
輸出:
123 換行 34 換行 45
ostringstream類
描述:把其他類型的數(shù)據(jù)寫入流(往流中寫入數(shù)據(jù)),支持<<操作
- ostringstream的構(gòu)造函數(shù)原形:
- ostringstream::ostringstream(string str);
初始化:使用字符串進(jìn)行初始化
- ostringstream ostr("1234");
- ostr.str("1234");//把字符串"1234"存入字符串流中
舉例:
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- //初始化輸出字符串流ostr
- ostringstream ostr("1234");
- cout<<ostr.str()<<endl;//輸出1234
-
- ostr.put('5');//字符4頂替了1的位置
- cout<<ostr.str()<<endl;//輸出5234
-
- ostr<<"67";//字符串67替代了23的位置,輸出5674
- string str = ostr.str();
- cout<<str<<endl;
- system("pause");
- return 1;
- }
stringstream類
描述:是對(duì)istringstream和ostringstream類的綜合,支持<<, >>操作符,可以進(jìn)行字符串到其它類型的快速轉(zhuǎn)換
- stringstream的構(gòu)造函數(shù)原形如下:
- stringstream::stringstream(string str);
初始化:使用字符串進(jìn)行初始化
- stringstream str("1234");
- str.str("1234");//把字符串"1234"存入字符串流中
作用:
1、stringstream通常是用來做數(shù)據(jù)轉(zhuǎn)換的
2、將文件的所有數(shù)據(jù)一次性讀入內(nèi)存
舉例1:基本數(shù)據(jù)類型變字符串
- /*基本數(shù)據(jù)類型變字符串*/
- #include <fstream>
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- /*整型變字符串*/
- int n = 10;
- string str;
- stringstream stream;
-
- stream << n;
- stream >> str;
-
- cout<<str<<endl;
- stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");否則下面輸出10
-
- /*char* 變 string*/
- char cStr[10] = "china";
-
- stream << cStr;
- stream >> str;
-
- cout<<str<<endl;
- system("pause");
- return 1;
- }
}
舉例2:字符串變基本數(shù)據(jù)類型
- /*字符串變基本數(shù)據(jù)類型*/
- #include <fstream>
- #include <iostream>
- #include <sstream>
- using namespace std;
- int main()
- {
- /*字符串 變 double*/
- double n;
- string str = "12.5";
- stringstream stream;
-
- stream << str;
- stream >> n;
-
- cout<<n<<endl;
- stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");
-
- /*string 變 char* */
- string str1 = "china";
- char cStr[10];
-
- stream << str1;
- stream >> cStr;
-
- cout<<cStr<<endl;//輸出china
- system("pause");
- return 1;
- }
注意:
- #include <iostream>
- #include <sstream>
- using namespace std;
-
- int main(int argc,char *argv[])
- {
- std::stringstream stream;
- string str;
- while(1)
- {
- //clear(),這個(gè)名字讓很多人想當(dāng)然地認(rèn)為它會(huì)清除流的內(nèi)容。
- //實(shí)際上,它并不清空任何內(nèi)容,它只是重置了流的狀態(tài)標(biāo)志而已!
- stream.clear();
-
- // 去掉下面這行注釋,清空stringstream的緩沖,每次循環(huán)內(nèi)存消耗將不再增加!
- //stream.str("");
-
- stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";
- stream>>str;
-
- //測試輸出每次循環(huán),你的內(nèi)存消耗增加了多少!
- cout<<"Size of stream = "<<stream.str().length()<<endl;
- system("PAUSE");
- }
-
- system("PAUSE");
- return EXIT_SUCCESS;
- }
由于stringstream構(gòu)造函數(shù)會(huì)特別消耗內(nèi)存,似乎不打算主動(dòng)釋放內(nèi)存(或許是為了提高效率),但如果你要在程序中用同一個(gè)流,反復(fù)讀寫大量的數(shù)據(jù),將會(huì)造成大量的內(nèi)存消耗,因些這時(shí)候,需要適時(shí)地清除一下緩沖 (用 stream.str("") )。
另外不要企圖用 stream.str().resize(0),或 stream.str().clear() 來清除緩沖,使用它們似乎可以讓stringstream的內(nèi)存消耗不要增長得那么快,但仍然不能達(dá)到清除stringstream緩沖的效果,內(nèi)存的消耗還在緩慢的增長!,至于stream.flush(),則根本就起不到任何作用。
|