By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

Anyway to break input into tokens in STL?

P: n/a
Thank you, this problem has bothered me for several years.
I want to extract words from an input file stream. Once a word,
ifstream should use the operator >>. But by default, it's delimited
only by white spaces. I need it to consider all non-alphabet as
delimiters.
For example, input "Hello, year 2005's halloween!", will be extracted
one by one as: hello year s halloween
In c, I can use
char *strtok( char *strToken, const char *strDelimit );
to do this, but is there anything like it in STL string/stream? My
current solution is reading character by character and then judge by
isalpha(c). It's too awkward.

Oct 1 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Just use
replace_if( String.begin(), String.end(), bind2nd( equal_to<char>(), ' ,' ),
' ' );

for every character you would like to replace.

Then use stringstream and operator>> to extract.

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Atlas" <Ma*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Thank you, this problem has bothered me for several years.
I want to extract words from an input file stream. Once a word,
ifstream should use the operator >>. But by default, it's delimited
only by white spaces. I need it to consider all non-alphabet as
delimiters.
For example, input "Hello, year 2005's halloween!", will be extracted
one by one as: hello year s halloween
In c, I can use
char *strtok( char *strToken, const char *strDelimit );
to do this, but is there anything like it in STL string/stream? My
current solution is reading character by character and then judge by
isalpha(c). It's too awkward.

Oct 1 '05 #2

P: n/a
* Atlas:
Thank you, this problem has bothered me for several years.
I want to extract words from an input file stream. Once a word,
ifstream should use the operator >>. But by default, it's delimited
only by white spaces. I need it to consider all non-alphabet as
delimiters.
For example, input "Hello, year 2005's halloween!", will be extracted
one by one as: hello year s halloween
In c, I can use
char *strtok( char *strToken, const char *strDelimit );
to do this, but is there anything like it in STL string/stream? My
current solution is reading character by character and then judge by
isalpha(c). It's too awkward.


If you've solved it once then presumably you can just reuse that solution?

Anyway, since I hate iostreams I know little about them except their bad
points, including lack of support for much of their functionality with current
compilers (e.g. wcout and wcin not being present in g++'s standard library).

So I thought I could learn something by trying my hand at this.

And discovered that the std::ctype<char>::do_is function does not exist in the
library implementation supplied with MSVC 7.1 -- PJP, do you hear?

And I think that by that I learned enough, this time... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.