473,383 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Anyway to break input into tokens in STL?

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
2 1591
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Erwin Moller | last post by:
Hi all, Situation: I need arbitrary calculations to be done on certain columns in a table. The formula's are dynamical. I will replace certain values in the formulastring with their current...
1
by: Paul McGuire | last post by:
***This is of especial interest for those who are using the pyparsing module, and have defined grammars that make use of CaselessLiteral.*** One of the bugfix requests I recently got for...
4
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. ...
8
by: Ron | last post by:
Hi all: I have an asp.net (using SQLserver) page in a class registration application where a supervisor can register for multiple employees. If there are no errors, duplicates the program flows...
2
by: Carl Cerecke | last post by:
Well, it doesn't quite rule them all, but it is fast: About three times faster than using one function per state. Faster than using generators. Faster than using code objects. Some, possibly...
9
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
8
by: Gemma Fletcher | last post by:
Hi all :) I'm implementing a menu system for my assignment. User inputs command & an if/else switch statement executes the command. I'm just not quite sure the best way to take user input. ...
1
by: beatTheDevil | last post by:
Hello all, I have a question that concerns how C++ input streams (istream, ifstream, istringstream, etc.) behave using the extraction (>>) operator when at the end of a stream's contents. For...
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.