1/* 2 * Same as stringtok_h.txt, but doesn't (visiably) use C functions. 3*/ 4 5#include <string> 6 7// The std:: prefix is not used here, for readability, and a line like 8// "using namespace std;" is dangerous to have in a header file. 9 10template <typename Container> 11void 12stringtok (Container &container, string const &in, 13 const char * const delimiters = " \t\n") 14{ 15 const string::size_type len = in.length(); 16 string::size_type i = 0; 17 18 while ( i < len ) 19 { 20 // eat leading whitespace 21 i = in.find_first_not_of (delimiters, i); 22 if (i == string::npos) 23 return; // nothing left but white space 24 25 // find the end of the token 26 string::size_type j = in.find_first_of (delimiters, i); 27 28 // push token 29 if (j == string::npos) { 30 container.push_back (in.substr(i)); 31 return; 32 } else 33 container.push_back (in.substr(i, j-i)); 34 35 // set up for next loop 36 i = j + 1; 37 } 38} 39 40