C sstream 中處理字串

2021-08-23 12:37:37 字數 979 閱讀 1216

c++引入ostringstream、istringstream、stringstream這三個類,要使用他們建立物件就必須包含這個標頭檔案。

istringstream的建構函式原形如下:

istringstream::istringstream(string str);

它的作用是從string物件str中讀取字元,stringstream物件可以繫結一行字串,然後以空格為分隔符把該行分隔開來。

下面我們分離以空格為界限,分割乙個字串。

#include#include#includeint main()

while (is);

return 0;

}

程式輸出

iam 

coding

另外用vector也可以實現

#include #include#include #include using namespace std;

int main()

return 0;

}

補充知識點,自己積累學習

在型別轉換中使用模板

你可以輕鬆地定義函式模板來將乙個任意的型別轉換到特定的目標型別。例如,需要將各種數字值,如int、long、double等等轉換成字串,要使用以乙個string型別和乙個任意值t為引數的to_string()函式。to_string()函式將t轉換為字串並寫入result中。使用str()成員函式來獲取流內部緩衝的乙份拷貝:

templatevoid to_string(string & result,const t& t)

這樣使用convert():

double d;

string salary;

string s=」12.56」;

d=convert(s);//d等於12.56

salary=convert(9000.0);//salary等於」9000」

bash中字串處理

得到長度 x abcd 方法一 expr length x 4 方法二 echo 4 方法三 expr x 4 expr 的幫助 string regexp anchored pattern match of regexp in string 查詢子串 expr index x b 2 expr i...

MATLAB中字串處理

在matlab中 字串是用單引號括起來的字串行 若字串中有單引號,那就要用兩個單引號來表示 i m superman ans i m superman建立多行字串 列數要相同 wow sadf 2134 wow sadf 2134 wow 2,4 ans 4建立乙個字串向量,然後對該向量作如下處理 ...

字串處理 字串反轉

請原諒博主今天很閒,於是乎博主又開始更新微博了。這次要更新的問題是 編寫乙個函式,反轉乙個單詞的順序。例如 do or do not,there is no try.就要反轉成 try.no is there not,do or do 大家要認真看看這道題,這道題和大家想象的貌似有點不同。首先字串反...