1. /* strtok example */ 
  2. #include <stdio.h> 
  3. #include <string.h> 
  4.  
  5. int main () 
  6.   char str[] ="- This, a sample string."; 
  7.   char * pch; 
  8.   printf ("Splitting string \"%s\" into tokens:\n",str); 
  9.   pch = strtok (str," ,.-"); 
  10.   while (pch != NULL) 
  11.   { 
  12.     printf ("%s\n",pch); 
  13.     pch = strtok (NULL, " ,.-"); 
  14.   } 
  15.   return 0; 

 

 

 
  
  1. #include <iostream> 
  2. #include <sstream> 
  3. #include <string> 
  4. using namespace std; 
  5.  
  6. int main() 
  7.     string s("Somewhere down the road"); 
  8.     istringstream iss(s); 
  9.  
  10.     do 
  11.     { 
  12.         string sub; 
  13.         iss >> sub; 
  14.         cout << "Substring: " << sub << endl
  15.     } while (iss); 
  16.  
  17.     return 0; 

 

 

 
  
  1. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { 
  2.     std::stringstream ss(s); 
  3.     std::string item; 
  4.     while(std::getline(ss, item, delim)) { 
  5.         elems.push_back(item); 
  6.     } 
  7.     return elems; 
  8.  
  9.  
  10. std::vector<std::string> split(const std::string &s, char delim) { 
  11.     std::vector<std::string> elems; 
  12.     return split(s, delim, elems); 

 include stringsstream and vector

 

 

 
  
  1. #include <vector> 
  2. #include <string> 
  3. #include <sstream> 
  4.  
  5. using namespace std; 
  6.  
  7. int main() 
  8.     string str("Split me by whitespaces"); 
  9.     string buf; // Have a buffer string 
  10.     stringstream ss(str); // Insert the string into a stream 
  11.  
  12.     vector<string> tokens; // Create vector to hold our words 
  13.  
  14.     while (ss >> buf) 
  15.         tokens.push_back(buf);