返回列表 發帖

字串分割 (一)

本帖最後由 tonyh 於 2024-6-22 11:59 編輯

試將字串 "123.45.6789" 以 "." 作為分割的依據進行分割,並將分割結果存入一陣列,再垂直打印出來。

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  4. #include<algorithm>
  5. using namespace std;
  6. string str="123.45.6789";
  7. int data[50];
  8. stringstream ss;
  9. int main()
  10. {
  11.     //cout<<str<<endl;
  12.     replace(begin(str),end(str),'.',' ');
  13.     //cout<<str<<endl;
  14.     ss<<str;
  15.     int n, index=0;
  16.     while(ss>>n)
  17.     {
  18.         data[index]=n;
  19.         index++;
  20.     }
  21.     for(int i=0; i<index; i++)
  22.         cout<<data[i]<<endl;
  23.     return 0;
  24. }
複製代碼

返回列表