字符串流stringstream(頭文件sstream)

 

#include<iostream>
#include<sstream>
using namespace std;

int main()
{
    double a,b;
    istringstream is("12.3445 2.3");
    is>>a>>b;                                //這裏可以轉任意類型
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;

    // 字符串,連接
    string tt ("dddddd");
    stringstream ss ;

    for(int i =0;i<10;i++)
        ss<<tt;

    string out = ss.str().c_str();

    cout<<"b = "<<out <<endl;

    return 0;
}

多個參數字符串連接

#include <jni.h>
#include <string>
#include <sstream>
#include <exception>
#include<iostream>
using namespace std;


inline ostream &_concat(ostream &os){
    return os;
};
template <typename T1, typename... TT>
inline ostream &_concat(ostream &os,const T1 &t1, TT && ... tt){
    os<<t1;
    return _concat(os,tt...);
};

int main()
{
 
    // 字符串,連接
    string tt ("dddddd");
    stringstream ss ;
    //  all to string
    _concat(ss,tt,tt,tt);
    cout<<"out= "<<ss <<endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章