試將字串 "123.45.6789" 以 "." 作為分割的依據進行分割,並將分割結果存入一陣列。
 - #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- string str="123.45.6789";
- string res[50];
- string tmp="";
- int j=0;
- for(int i=0; i<str.length(); i++)
- {
- if(str[i]=='.')
- {
- res[j]=tmp;
- tmp="";
- j++;
- continue; //跳過當下的迴圈
- }
- tmp+=str[i];
- //cout<<str[i]<<endl;
- }
- res[j]=tmp;
- for(int i=0; res[i]!=""; i++)
- cout<<res[i]<<endl;
- system("pause");
- return 0;
- }
複製代碼 |