本帖最後由 tonyh 於 2022-12-15 19:35 編輯
試將字串 "123.45.6789" 以 "." 作為分割的依據進行分割,並將分割結果存入一陣列。
 - #include<bits/stdc++.h>
- using namespace std;
- string str="123.45.6789";
- int data[50];
- stringstream ss;
- int n;
- int main()
- {
- cin.tie(0);
- cin.sync_with_stdio(0);
- //cout<<str<<endl;
- replace(begin(str),end(str),'.',' ');
- //cout<<str<<endl;
- ss<<str;
- int index=0;
- while(ss>>n)
- {
- data[index]=n;
- index++;
- }
- for(int i=0; i<index; i++)
- cout<<data[i]<<endl;
- return 0;
- }
複製代碼- #include<bits/stdc++.h>
- using namespace std;
- string str="123.45.6789";
- stringstream ss;
- int n;
- int main()
- {
- cin.tie(0);
- cin.sync_with_stdio(0);
- //cout<<str<<endl;
- replace(str.begin(),str.end(),'.',' ');
- //cout<<str<<endl;
- ss<<str;
- while(ss>>n)
- cout<<n<<endl;
- return 0;
- }
複製代碼- #include<bits/stdc++.h>
- using namespace std;
- string str="123.45.6789";
- stringstream ss;
- int n, index=0, data[100001];
- int main()
- {
- cin.tie(0);
- cin.sync_with_stdio(0);
- replace(str.begin(),str.end(),'.',' ');
- ss<<str;
- while(ss>>n)
- {
- data[index]=n;
- index++;
- }
- for(int i=0; i<index; i++)
- cout<<data[i]<<endl;
- return 0;
- }
複製代碼- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- string str="123.45.6789";
- str+=".";
- string res[50];
- int index=0;
- string tmp="";
- for(int i=0; i<str.size(); i++)
- {
- if(str[i]=='.')
- {
- res[index]=tmp;
- tmp="";
- index++;
- }else
- {
- tmp+=str[i];
- }
- }
- for(int i=0; res[i]!=""; i++)
- cout<<res[i]<<endl;
- system("pause");
- return 0;
- }
複製代碼 |