cpp split string by space code example
Example 1: cpp split string by space
std::vector string_split(const std::string& str) {
std::vector result;
std::istringstream iss(str);
for (std::string s; iss >> s; )
result.push_back(s);
return result;
}
Example 2: string split by space c++
// Extract the first token
char * token = strtok(string, " ");
// loop through the string to extract all other tokens
while( token != NULL ) {
printf( " %s\n", token ); //printing each token
token = strtok(NULL, " ");
}
return 0;