C++ string class tokenizer method?

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

Is it that I am blurry eyed, or is it indeed that the C++ string class
has no tokenizer method defined? I have defined my own functions [that
I have posted to comp.sources.d], but I would prefer to use the
standard functions, if available.

Thanks
Bhat
 
L

Larry I Smith

Generic said:
Is it that I am blurry eyed, or is it indeed that the C++ string class
has no tokenizer method defined? I have defined my own functions [that
I have posted to comp.sources.d], but I would prefer to use the
standard functions, if available.

Thanks
Bhat

Not in the same sense as the "C" strtok().
You can use c_str() to create a "C" nul-terminated
string from a std::string, then use strtok() on
that nul-terminated string.

char * cstr = strdup(cppstr.c_str());
// use strtok() on 'cstr'
// then free() 'cstr'

The C++ string class has many methods for parsing:
find(), find_first_of(), find_first_not_of(),
find_last_of(), find_last_not_of(), rfind(),
replace(), swap(), substr(), etc, etc, etc...

Given all of the methods of std::string, I've
never had to use strtok() with them.

Regards,
Larry
 
J

Jack Klein

Generic said:
Is it that I am blurry eyed, or is it indeed that the C++ string class
has no tokenizer method defined? I have defined my own functions [that
I have posted to comp.sources.d], but I would prefer to use the
standard functions, if available.

Thanks
Bhat

Not in the same sense as the "C" strtok().
You can use c_str() to create a "C" nul-terminated
string from a std::string, then use strtok() on
that nul-terminated string.

char * cstr = strdup(cppstr.c_str());

Just wanted to point out that:

1. There is no C or C++ standard library function strdup().

2. It is rather inadvisable to create a function of your own with
that name, seeing as how names beginning with "str" followed by a
lower case letter are reserved in many contexts in C++. And in all
contexts as a function name in C.
 
L

Larry I Smith

Jack said:
Generic said:
Is it that I am blurry eyed, or is it indeed that the C++ string class
has no tokenizer method defined? I have defined my own functions [that
I have posted to comp.sources.d], but I would prefer to use the
standard functions, if available.

Thanks
Bhat
Not in the same sense as the "C" strtok().
You can use c_str() to create a "C" nul-terminated
string from a std::string, then use strtok() on
that nul-terminated string.

char * cstr = strdup(cppstr.c_str());

Just wanted to point out that:

1. There is no C or C++ standard library function strdup().


Maybe, but I've never seen a "C" <string.h> (aka <cstring>) that
didn't have a strdup() - at least on MS Windows, Linux, & Solaris.
strtok(), strdup(), strlen(), strcpy(), etc are all valid "C"
functions that may be used in C++ when <string.h> or <cstring>
is included.

2. It is rather inadvisable to create a function of your own with
that name, seeing as how names beginning with "str" followed by a
lower case letter are reserved in many contexts in C++. And in all
contexts as a function name in C.

Regards,
Larry
 
R

Rolf Magnus

Larry said:
The C++ string class has many methods for parsing:
find(), find_first_of(), find_first_not_of(),
find_last_of(), find_last_not_of(), rfind(),
replace(), swap(), substr(), etc, etc, etc...

And there are stringstreams.
 
D

Default User

Larry said:
Jack Klein wrote:



Maybe, but I've never seen a "C" <string.h> (aka <cstring>) that
didn't have a strdup() - at least on MS Windows, Linux, & Solaris.
strtok(), strdup(), strlen(), strcpy(), etc are all valid "C"
functions that may be used in C++ when <string.h> or <cstring>
is included.


Three out the four you mention are standard C library functions that
are guaranteed to be available. However, strdup() is not. Regardless of
whether it's a common extension or not, we try very hard to stick to
the standard language in this newsgroup.



Brian
 
L

Larry I Smith

Default said:
Three out the four you mention are standard C library functions that
are guaranteed to be available. However, strdup() is not. Regardless of
whether it's a common extension or not, we try very hard to stick to
the standard language in this newsgroup.



Brian

Yes, I know, but on occasion real-world programs have to
use functions that are not in the Standard (read, write, socket,
fork, poll, select, etc). There's nothing wrong with that.

Regards,
Larry
 
D

Default User

Larry said:
Default User wrote:
Yes, I know, but on occasion real-world programs have to
use functions that are not in the Standard (read, write, socket,
fork, poll, select, etc). There's nothing wrong with that.

There is in this newgroup. Most of those are necessary for certain
platform-specific purposes. Platform-specific programs are best left to
the appropriate newsgroup.

It also doesn't apply to your use of strdup(). One can write that in
perfectly standard fashion:

char *s = new char[strlen(str.c_str() + 1];

strcpy (s, str.c_str());



Brian
 
L

Larry I Smith

Default said:
Default User wrote:
Yes, I know, but on occasion real-world programs have to
use functions that are not in the Standard (read, write, socket,
fork, poll, select, etc). There's nothing wrong with that.

There is in this newgroup. Most of those are necessary for certain
platform-specific purposes. Platform-specific programs are best left to
the appropriate newsgroup.

It also doesn't apply to your use of strdup(). One can write that in
perfectly standard fashion:

char *s = new char[strlen(str.c_str() + 1];

strcpy (s, str.c_str());



Brian

I think you're making WAY too much out of this strdup() issue.
Just because strdup() is not in the Standard, doesn't mean it can't
be mentioned. Boost is mentioned all of the time - and NO,
there's no difference between the two, non-Standard is
non-Standard, whether it be Boost or strdup(). So what.

It's available (almost) everywhere; so I mentioned it as a
possible solution to the OP's question.

It's no big deal. Without C/C++ methods NOT in the Standards,
there wouldn't be any networking, GUI, etc. I'm surprised that
merely mentioning the ubiquitous strdup() has upset you so - sorry.

If you look at my orifinal post, you'll see that I'm well
aware of the alternatives. After 30 years in Information
Technology, I have some idea about how everything works.

Have a nice day.

Regards,
Larry
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top