std::string equivalent of strtok ?

L

Leslaw Bieniasz

Hi,

Is there any equivalent of strtok() implemented within
std::string ? I need to do some parsing of std::string,
and decompose it into tokens separated by certain delimiting
characters. I cannot find relevant functions.
Also I don't see options for this within stringstream classes.

Leslaw
 
V

Victor Bazarov

Leslaw said:
Is there any equivalent of strtok() implemented within
std::string ? I need to do some parsing of std::string,
and decompose it into tokens separated by certain delimiting
characters. I cannot find relevant functions.
Also I don't see options for this within stringstream classes.

I vaguely recall that Boost had some tokeniser classes. Check it out.

V
 
M

Maxim Yegorushkin

Is there any equivalent of strtok() implemented within
std::string ? I need to do some parsing of std::string,
and decompose it into tokens separated by certain delimiting
characters. I cannot find relevant functions.

std::string::find_first_of/find_first_not_of can be used to achieve what
you want. Something like:

template<class Functor>
void for_each_token(
std::string const& input
, char const* separators
, Functor functor
)
{
char const* data = input.data();
size_t token_beg = 0;
for(;;) {
token_beg = input.find_first_not_of(separators, token_beg);
if(std::string::npos == token_beg) // no more tokens
return;
size_t token_end = input.find_first_of(separators, token_beg);
if(std::string::npos == token_end) // no more tokens
return;

// pass token iterators to the functor
functor(data + token_beg, data + token_end);

token_beg = token_end;
}
}
 
J

Jorgen Grahn

.
Is there any equivalent of strtok() implemented within
std::string ? I need to do some parsing of std::string,
and decompose it into tokens separated by certain delimiting
characters. I cannot find relevant functions.
Also I don't see options for this within stringstream classes.

I did the same search, and concluded that I had to write my own
(adding a dependency to Boost was not an option for me).

So I did a few variations on

vector<string> split(const string&);

with and without blankspace-trimming, with and without
a maximum split count, kind of like Perl's split function.
Returning the result in a vector is usually fast enough.

/Jorgen
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top