我参与了不同的编码竞赛,因此无法使用python导致它的执行时间太慢,但我真的很喜欢input.split()的东西,所以我试图实现自己的分裂.这是我想出的:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<string> splt(string s){
vector<string> ans={};
for(int i=0;i<s.size();i++){
string str="";
while(s[i]!=' '){
str+=s[i];
i++;
}
ans.push_back(str);
}
return ans;
}
int main(){
string s;
getline(cin, s);
vector<string> ans=splt(s);
for(auto i:ans)
cout<<i<<", ";
}
Run Code Online (Sandbox Code Playgroud)
但是函数返回的向量经常(但并非总是)最后有一些垃圾.非常感谢帮助纠正我的代码,以及将字符串拆分成数组的其他一些操作.
PS对不起,如果我的英语不好,我来自俄罗斯,甚至没有完成学业:)