c 2b 2b split string by space

Solutions on MaxInterview for c 2b 2b split string by space by the best coders in the world

showing results for - "c 2b 2b split string by space"
Elle
20 Mar 2016
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"));
Jessica
27 Jan 2020
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}
Matías
24 Feb 2016
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); 
Ricardo
14 Feb 2019
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
c 2b 2b split string by space