cpp split string by space

Solutions on MaxInterview for cpp split string by space by the best coders in the world

showing results for - "cpp split string by space"
Youssef
18 Mar 2019
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"));
Liah
06 May 2019
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}
María Alejandra
24 Nov 2020
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;
similar questions
queries leading to this page
cpp split string by space