Tokenize a string: Difference between revisions
Content added Content deleted
(Ada) |
|||
Line 55: | Line 55: | ||
{ |
{ |
||
using namespace std; |
using namespace std; |
||
// Skip delims at beginning |
// Skip delims at beginning, find start of first token |
||
string::size_type lastPos = str.find_first_not_of(delims, 0); |
string::size_type lastPos = str.find_first_not_of(delims, 0); |
||
// Find |
// Find next delimiter @ end of token |
||
string::size_type pos = str.find_first_of(delims, lastPos); |
string::size_type pos = str.find_first_of(delims, lastPos); |
||
Line 67: | Line 67: | ||
// Found a token, add it to the vector. |
// Found a token, add it to the vector. |
||
tokens.push_back(str.substr(lastPos, pos - lastPos)); |
tokens.push_back(str.substr(lastPos, pos - lastPos)); |
||
// Skip delims. Note the "not_of" |
// Skip delims. Note the "not_of". this is beginning of token |
||
lastPos = str.find_first_not_of(delims, pos); |
lastPos = str.find_first_not_of(delims, pos); |
||
// Find next |
// Find next delimiter at end of token. |
||
pos = str.find_first_of(delims, lastPos); |
pos = str.find_first_of(delims, lastPos); |
||
} |
} |