我创建了将其拆分char*
为的函数,vector
但问题是在运行此方法后,此方法的所有元素vector
都是输入行中的最后一个元素。
例子:
输入:abc def ghi
向量:ghi, ghi, ghi
vector <const char*> dane;
void split(char* str, char* sep){
char* cstr = str;//str.c_str konwersja str na char*
char* current;
current = strtok(cstr, sep);
string s;
while(current != NULL){
s = current;
int foundF = s.find_first_not_of("-.\"-\\/!,`");
int foundL = s.find_last_not_of("-.\"-\\/!,`");
if(foundL==string::npos)//find first or last zwrocilo nulla
foundL = s.length();
if(foundF==string::npos)
foundF = 0;
s = s.substr(foundF, foundL + 1);
const char* c = s.c_str();
dane.push_back(c);
current=strtok(NULL, sep);
}
}
int main(){
char* c = new char[256];
cin.getline(c,256);
cout<<c<<endl;
split(c, " ");
for(int i = 0; i < dane.size(); i++){
cout<<dane.at(i)<<endl;
}
return 0;
}