没有找到cpp内置的字符串分割函数,如果有的话希望同道指点一下。
#include<iostream>
#include<string>
#include<vector>
#include<sstream> // 需要用到的头文件
using namespace std;
void stringSplit(string astr, const char split, vector<string>& res){
istringstream ss(astr); // 输入流
string token; // 保存每个待分割的字符串
while(getline(ss,token,split)){ // 以split作为分隔符
res.push_back(token);
}
}
int main(){
string teststr = "hablee and yuki were going to be married but they were not"
" because of the Covid-19 easily break their relationship";
vector<string> ans;
stringSplit(teststr,' ',ans);
// 输出结果
for (string item:ans){
cout<<item<<"*";
}
return 0;
}