STL之sstream的用法

STL之sstream的用法

在這裏插入圖片描述
說在前面:

庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。另外,每個類都有一個對應的寬字符集版本。注意,使用string對象來代替字符數組。這樣可以避免緩衝區溢出的危險。而且,傳入參數和目標對象的類型被自動推導出來,即使使用了不正確的格式化符也沒有危險。

使用時必須加上#include<sstream>

  • stringstream 對象用於輸入一行字符串,以 空格 爲分隔符把該行分隔開來

    #include <iostream>
    #include <stack>
    #include <sstream>
    using namespace std;
    int main()
    {
        string str= "hello world I am very happy!";                           
        stringstream sstream(str);                                              //sstream<<
        while (sstream)
        {
            string substr;
            sstream>>substr;
            cout << substr <<" ";    //也可vec.push_back(substr);
        } 
        return 0;
    }
    //  輸出詳解:
    hello world I am very happy!
    //  大家可能會問這有什麼用,如果配上棧結構便能將這個逆序輸出:
    #include <iostream>
    #include <stack>
    #include <sstream>
    using namespace std;
    int main()
    {
        string str= "hello world I am very happy !",tmp;                           
        stringstream sstream(str);                                            
        stack<string>  s;
        while(sstream>>tmp)
        {
            s.push(tmp);
        }
        while(!s.empty())
        {
            cout<<s.top();
            s.pop();
            if(s.size()!=0)
            cout<<" ";
        }
        cout<<endl;
        return 0;
    }
    //  輸出結果:
    ! happy very am I world hello
    
  • 類型轉換

    利用stringstream進行常見類型轉換

    ​ (int、long、float、double、long double、long long)

/-----string轉int-----/
string str= "123456789";                           
stringstream sstream(str);                                            
int tmp;
sstream>>tmp;
cout<<tmp<<endl;     //  輸出結果123456789
/-----int轉string-----/
   int tmp=123456;
    stringstream t;
    string s;
    t<<tmp;
    t>>s;
    cout<<s<<endl;  //  輸出結果123456
/-----intchar-----/
    int tmp=123456;
    stringstream t;
    char s[10];
    t<<tmp;
    t>>s;
    for(int i=0;i<strlen(s);i++)
        cout<<s[i];   //  輸出結果123456
/-----charint-----/
    stringstream t;
    char s='8';
    int tmp;
    t<<s;
    t>>tmp;
    cout<<tmp;
    return 0;  //  輸出8;
  • 利用to_string() 進行常見類型轉換, 返回值爲string

    函數原型:(C++11纔有這個函數)
    string to_string (int val);
    string to_string (long val);
    string to_string (long long val);
    string to_string (unsigned val);
    string to_string (unsigned long val);
    string to_string (unsigned long long val);
    string to_string (float val);
    string to_string (double val);
    string to_string (long double val);

    功能:

    將數值轉化爲字符串。返回對應的字符串。

    例子:

  • s1=to_string(10.5);                        //double到string
    s2=to_string(123);                        //int到string
    s3=to_string(true);                     //bool到string
    
    
    #include <iostream>
    #include <stack>
    #include <sstream>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    int main()
    {
        string s1,s2,s3;
        s1=to_string(10.5); //double到string
        s2=to_string(123);//int到string
        s3=to_string(true);//bool到string
        cout<<fixed<<showpoint<<setprecision(1);
        cout<<s1<<" "<<s2<<" "<<s3<<endl;
        return 0;
    }
    //  10.500000 123 1
    
    
    重複利用stringstream對象

    如果你打算在多次轉換中使用同一個stringstream對象,記住再每次轉換前要使用clear()方法;

    在多次轉換中重複使用同一個stringstream(而不是每次都創建一個新的對象)對象最大的好處在於效率。stringstream對象的構造和析構函數通常是非常耗費CPU時間的。

    #include <iostream>
    #include <stack>
    #include <sstream>
    #include <cstring>
    using namespace std;
    int main()
    {                                         
        stringstream stream;
        int first;
        string second;
        stream<< "456"; //插入字符串
        stream >> first; //轉換成int
        cout << first << endl;
        stream.clear(); //在進行多次轉換前,必須清除stream,否則第二個數據可能輸出亂碼
        stream << "true"; //插入bool值
        stream >> second; //提取出int
        cout << second << endl;
        return 0;
    }
    //  輸出結果:
    456
    true
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章