string
aをchar
array に変換してから、 a に戻し、 に変換string
してい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 ファイルです。1 つの関数は char を文字列ベクトルに分割し、もう 1 つは印刷です。
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;
}
}