STL usage of sstream

STL usage of sstream

说在前面:

Library defines three types: istringstream, ostringstream and the stringstream, are used for the input stream, and output the input and output operations. Further, there is a corresponding wide-character version of each class set. note, Use string object instead of an array of characters. This avoids the risk of buffer overflow. Furthermore, parameters passed the target object type is automatically derived, even if an incorrect format identifier is no danger.

使用时必须加上#include<sstream>

  • stringstream object is used to input his string, a space for the separator to be separated from the row

    #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
    
  • Type Conversion

    Stringstream carried out using the common type conversion

    ​ (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
/-----int转char-----/
    int tmp=123456;
    stringstream t;
    char s[10];
    t<<tmp;
    t>>s;
    for(int i=0;i<strlen(s);i++)
        cout<<s[i];   //  输出结果123456
/-----char转int-----/
    stringstream t;
    char s='8';
    int tmp;
    t<<s;
    t>>tmp;
    cout<<tmp;
    return 0;  //  输出8;
  • Using the to_string () for a common type of conversion, the return value 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);

    Features:

    The value into a string. Returns a string corresponding.

    example:

  • 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
    
    
    Reuse objects stringstream

    If you plan to use the same object multiple times stringstream conversion, remember again before each conversion to use clear () method;

    Reuse the same stringstream in multiple conversions in (but not always create a new object) the biggest benefit is efficiency target. Stringstream construction and destructor object is usually very time-consuming 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
    

Guess you like

Origin www.cnblogs.com/Yqifei/p/12660054.html