C++ stringstream 介紹與示例


C++引入了ostringstream、istringstream、stringstream這三個類,要使用他們創建對象就必須包含<sstream>頭文件,通常是用來做數據轉換的。
  • istringstream類用於執行C++風格的串流的輸入操作。 
  • ostringstream類用於執行C風格的串流的輸出操作。
  • strstream類同時可以支持C風格的串流的輸入輸出操作。 
istringstream類是從istream和stringstreambase派生而來,ostringstream是從ostream和 stringstreambase派生而來, stringstream則是從iostream類和stringstreambase派生而來。
 
他們的繼承關系如下圖所示:
 
示例一:istringstream的使用
 1 #include <iostream>
 2 #include <sstream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     istringstream istr;
 9     istr.str("1 56.7");
10     //上述兩個過程可以簡單寫成 istringstream istr("1 56.7");
11     cout << istr.str() << endl;
12     int a;
13     float b;
14     istr >> a;
15     cout << a << endl;
16     istr >> b;
17     cout << b << endl;
18     return 0;
19 }

輸出:

1 56.7
1
56.7

說明:上例中,構造字符串流的時候,空格會成為字符串參數的內部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點,字符串的空格成為了整型數據與浮點型數據的分解點,利用分界獲取的方法我們事實上完成了字符串到整型對象與浮點型對象的拆分轉換過程。str()成員函數的使用可以讓istringstream對象返回一個string字符串。

 

示例二:ostringstream的使用

 1 #include<iostream>
 2 #include<sstream>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 int main(){
 8     ostringstream ostr;
 9     //ostr.str("abc"); //如果構造的時候設置了字符串參數,那么增長操作的時候不會從結尾開始增加,而是修改原有數據,超出的部分增長
10     ostr.put('d');
11     ostr.put('e');
12     ostr<<"fg";
13 
14     string gstr = ostr.str();
15     cout << gstr << endl;
16     }

輸出:

defg

說明:通過put()或者左移操作符可以不斷向ostr插入單個字符或者是字符串,通過str()函數返回增長過后的完整字符串數據,但值 得注意的一點是,當構造的時候對象內已經存在字符串數據的時候,那么增長操作的時候不會從結尾開始增加,而是修改原有數據,超出的部分增長。

 

示例三:stringstream的使用

 1 #include<iostream>
 2 #include<sstream>
 3 #include<string>
 4 using namespace std;
 5 
 6 int main(){
 7     stringstream ostr("ccc");
 8     ostr.put('d');
 9     ostr.put('e');
10     ostr<<"fg";
11     string gstr = ostr.str();
12     cout << gstr << endl;
13 
14     char a;
15     // string a; // "defg"
16     ostr>>a;
17     cout << a << endl;
18     }

輸出:

defg
d

 

示例四:使用stringstream對象簡化類型轉換

 1 #include<iostream>
 2 #include<sstream>
 3 #include<string>
 4 using namespace std;
 5 
 6 template<class T>
 7 void to_string(string & result, const T & t)
 8 {
 9      ostringstream oss;//創建一個流
10      oss<<t;//把值傳遞到流中
11      result=oss.str();//獲取轉換后的字符轉並將其寫入result
12 }
13 
14 template<class out_type, class in_value>
15 out_type convert(const in_value & t)
16 {
17     stringstream ss;
18     ss<<t;
19     out_type result;
20     ss>>result;
21     return result;
22 }
23 
24 int main()
25 {
26     stringstream sstr;
27     //--------int轉string-----------
28     int a = 100;
29     string str;
30     sstr << a;
31     sstr >> str;
32     cout << str << endl;
33 
34     //--------string轉char[]--------
35     sstr.clear(); //如果你想通過使用同一stringstream對象實現多種類型的轉換,請注意在每一次轉換之后都必須調用clear()成員函數。
36     string name = "abcde";
37     char cname[200];
38     sstr << name;
39     sstr >> cname;
40     cout << cname << endl;
41 
42     //--------調用函數--------
43     string s1;
44     to_string(s1,10.5);//double到string
45     cout << s1 << endl;
46 
47     double d;
48     string s="12.34";
49     d = convert<double>(s);
50     cout << "d " << d << endl;
51 
52     string salary;
53     salary = convert<string>(100.0);
54     cout << "salary " << salary << endl;
55 }

輸出:

100
abcde
10.5
d 12.34
salary 100

說明:

函數模板to_string():可以輕松地將多種數值轉換成字符串。

函數模板convert():定義一個通用的轉換模板,用於任意類型之間的轉換。convert()含有兩個模板參數out_type和in_value,功能是將in_value值轉換成out_type類型。

如果想通過使用同一stringstream對象實現多種類型的轉換,請注意在每一次轉換之后都必須調用clear()成員函數。

 

 

 

引用:https://blog.csdn.net/JoeBlackzqq/article/details/7032703

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM