我正在将 a 转换string
为char
数组,而不是转换回 astring
和vector
. 当我尝试打印时,我得到了这个:
this
is
the
sentence iuִִ[nu@h?(h????X
以及更多。这是代码:
int main(int argc, char *argv[]){
string s ="this is the sentence";
char seq[sizeof(s)];
strcpy(seq, "this is the sentence");
vector<string> vec = split(seq);
printWords(vec);
return 0;
}
这是 func.cpp 文件。一个函数将字符拆分为字符串向量,另一个是打印:
vector<string> split(char sentence[]){
vector<string> vecto;
int i=0;
int size= strlen(sentence);
while((unsigned)i< size){
string s;
char c =' ';
while(sentence[i]!=c){
s=s+sentence[i];
i+=1;
}
vecto.push_back(s);
i+=1;
}
return vecto;
}
void printWords(vector<string> words){
int i=0;
while ((unsigned)i<words.size()){
string s = words.at(i);
cout << words.at(i) << endl;
i+=1;
}
}