采用string字符串str存储,最后提取的单词采用vector<string> 容器words存储。对应程序如下
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void solve(string basicString, vector<string> &words);
int main() {
string str ="The following code compute the intersection of two arrays";
cout<<" "<<str<<endl;
vector<string> words;
solve(str,words);
vector<string>::iterator it;
for(it=words.begin();it!=words.end();it++){
cout<<" "<<*it<<endl;
}
}
void solve(string basicString, vector<string> &words) {
string w;
int i=0,j=0;
j = basicString.find(" ");
while (j!=-1){
w = basicString.substr(i,j-i);
words.push_back(w);
i = j+1;
j = basicString.find(" ",i);
}
if(i<=basicString.length()-1){
w = basicString.substr(i);
words.push_back(w);
}
}