I am trying to split a char* based on a delimiter using nested vectors however the last word of the char* seems to not be added to the vector>
vector<vector<char>> split(char* word, const char de){
vector<vector<char>> words;
vector<char> c_word;
while(*word){
if(*word == de){
words.push_back(c_word);
c_word.clear();
word++;
continue;
}
c_word.push_back(*word);
word++;
}
return words;
}
Example usage:
int main() {
char *let = "Hello world!";
vector<vector<char>> words = split(let, ' ');
for(int x = 0;x < words.size();x++){
for(int y = 0;y < words[x].size();y++){
cout << words[x][y];
}
cout << endl;
}
}
This would print only Hello
std::vector<std::vector<char>>
instead ofstd::vector<string>
? Andchar*
instead ofstd::string
? – Some programmer dude Oct 20 '17 at 19:12c_word
when the loop is finished. – Johnny Mopp Oct 20 '17 at 19:12