1std::string s = "What is the right way to split a string into a vector of strings";
2std::stringstream ss(s);
3std::istream_iterator<std::string> begin(ss);
4std::istream_iterator<std::string> end;
5std::vector<std::string> vstrings(begin, end);
6std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
1std::vector<std::string> string_split(const std::string& str) {
2 std::vector<std::string> result;
3 std::istringstream iss(str);
4 for (std::string s; iss >> s; )
5 result.push_back(s);
6 return result;
7}
1std::string s = "split on whitespace ";
2std::vector<std::string> result;
3std::istringstream iss(s);
4for(std::string s; iss >> s; )
5 result.push_back(s);
1// Extract the first token
2char * token = strtok(string, " ");
3// loop through the string to extract all other tokens
4while( token != NULL ) {
5 printf( " %s\n", token ); //printing each token
6 token = strtok(NULL, " ");
7}
8return 0;