J
John Fly
I've used standard functions like find_first_of and find_first_not_of
for some time. I was about to implement a quick string tokenizer
similar to the example below when I ran across an old thread stating
possible undefined behavior or possible portability issues with using
std::string::npos
I did not see a direct reason for the warning, and want to make sure
I'm not setting my programs up for future failure by using npos.
-- Are there any issues to using npos in situations like the given
example? If so can someone explain?
void GetTokens(const std::string& input, const std::string& delims,
std::vector<std::string>& tokens)
{
tokens.clear();
std::string::size_type beg_index , end_index;
beg_index = input.find_first_not_of(delims);
while (beg_index != std::string::npos)
{
end_index = input.find_first_of(delims,beg_index);
if (end_index == std::string::npos) end_index = input.length();
tokens.push_back( input.substr(beg_index,end_index-beg_index) );
beg_index = input.find_first_not_of(delims,end_index);
}
}
*Related issue: Besides using boost functionality, is there a
cleaner/better way to tokenize strings?
for some time. I was about to implement a quick string tokenizer
similar to the example below when I ran across an old thread stating
possible undefined behavior or possible portability issues with using
std::string::npos
I did not see a direct reason for the warning, and want to make sure
I'm not setting my programs up for future failure by using npos.
-- Are there any issues to using npos in situations like the given
example? If so can someone explain?
void GetTokens(const std::string& input, const std::string& delims,
std::vector<std::string>& tokens)
{
tokens.clear();
std::string::size_type beg_index , end_index;
beg_index = input.find_first_not_of(delims);
while (beg_index != std::string::npos)
{
end_index = input.find_first_of(delims,beg_index);
if (end_index == std::string::npos) end_index = input.length();
tokens.push_back( input.substr(beg_index,end_index-beg_index) );
beg_index = input.find_first_not_of(delims,end_index);
}
}
*Related issue: Besides using boost functionality, is there a
cleaner/better way to tokenize strings?