Anyway to break input into tokens in STL?

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.
 
M

Moonlit

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
 
A

Alf P. Steinbach

* 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... ;-)
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top