Issues with npos? writing a non-boost string tokenizer

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?
 
A

Archmagus

As far as I know std::string::npos is part of the Standard C++ library.
As long as you compile it with a compiler that follows the C++
standards, you should run into no portability problems...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top