strtok() functionality in C++

T

Tydr Schnubbis

Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}
 
M

Mike Wahler

Tydr Schnubbis said:
Is there a "C++ way" of doing what strtok() does,

Yes. strtok() (It's as much a part of C++ as it is of C).
or should I just convert string objects into char arrays and use strtok()?
I couldn't find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}

That depends entirely upon your needs.

You can indeed tokenize a std::string object in various
ways. One tool to that end is std::string::find_first_of()
(which will locate a delimiter from a specified set,
but doesn't replace it with a NULL as does strtok())

-Mike
 
J

John Harrison

Tydr said:
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}

No it should not compile. strtok modifies it's argument and c_str()
returns a const char*.

There is no built in C++ string tokeniser. It's not too hard to write
your own especially if you keep it simple, or you could get a third
party one, for instance

http://www.boost.org/libs/tokenizer/index.html

john
 
I

int2str

Tydr said:
Is there a "C++ way" of doing what strtok() does, or should I just
convert string objects into char arrays and use strtok()? I couldn't
find a C++ way that is as straight forward as using strtok().

Would the following be considered "good" C++ style?

s = strtok(line.c_str(), " \t\n");
while (s) {
/* use s here */
s = strtok(NULL, " \t\n");
}

If whitespace is all you need as separators, then this should work for
you:

std::istringstream iss( line );
std::string token;
while( iss >> token )
{
// use 'token' here
}
 
T

Tydr Schnubbis

If whitespace is all you need as separators, then this should work for
you:

std::istringstream iss( line );
std::string token;
while( iss >> token )
{
// use 'token' here
}
Thanks, that was just what I was looking for. Simpler and cleaner than
using strtok, as long as you don't require the extra flexibility.
 
N

Niklas Norrthon

Tydr Schnubbis said:
Thanks, that was just what I was looking for. Simpler and cleaner
than using strtok, as long as you don't require the extra flexibility.

And if you do need the extra flexibility: (untested)

string next_token(const string& line, const string& delim, string::size_type& first)
{
string::size_type last = line.find_first_of(delim, first);
string next(line.substr(first, last));
first = last;
return next;
}

/Niklas Norrthon
 

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

Similar Threads

Can't solve problems! please Help 0
Why does strcat mess up the tokens in strtok (and strtok_r)? 92
strtok 7
strtok equiavalent in C++ ? 5
strtok problem 16
strtok question 6
strtok 6
use of strtok( ) 5

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top