er  dev
cppTokenizer.h
1 #ifndef CPPTOKENIZER_H
2 #define CPPTOKENIZER_H
3 
4 #include <vector>
5 #include <string>
6 
7 std::vector<std::string>
8 tokenize(const std::string & str, const std::string & delimiters)
9 {
10  std::vector<std::string> tokens;
11 
12  // Skip delimiters at beginning.
13  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
14 
15  // Find first "non-delimiter".
16  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
17 
18  while (std::string::npos != pos || std::string::npos != lastPos)
19  {
20  // Found a token, add it to the vector.
21  tokens.push_back(str.substr(lastPos, pos - lastPos));
22 
23  // Skip delimiters. Note the "not_of".
24  lastPos = str.find_first_not_of(delimiters, pos);
25 
26  // Find next "non-delimiter".
27  pos = str.find_first_of(delimiters, lastPos);
28  }
29 
30  return tokens;
31 }
32 
33 #endif // CPPTOKENIZER_H