Hello all, I'm trying to write a function which given a std::string
parses the string by breaking the sentance up by whitespace (\t, ' ',
\n) and returns the result as a vector of strings. Here's what I have
so far:
std::vector<std ::string> tokenize (std::string foo)
{
std::istringstr eam s (foo);
std::vector <std::string> v;
std::string tok;
for (;;) // infinite loop
{
// try to extract token
s >> tok;
// if string was read, push onto vector else break out
of loop
if (s.good())
v.push_back(tok );
else
break;
}
return v;
}
The problem is that now if given a string that doesn't have whitespace
at the end (ex - "this is a string"), the last token will be
passed up because s.good() will return false not when the last
extraction failed, but when there is no further input in the
istringstream. If I restructure the loop as:
while (s.good())
{
s >> tok;
v.push_back(tok );
}
then if there is whitespace after the last token (ex - "this is a
string "), then v will end up with the last token repeated, as
an extraction when there is only whitespace in the istringstream will
not modify the tok variable (so it keeps it's old value which was the
last token successfully read, and then v pushes this onto the end of the
vector).
Any suggestions?
Thanks,
Adam Parkin 1 7032
Adam Parkin wrote: Hello all, I'm trying to write a function which given a std::string parses the string by breaking the sentance up by whitespace (\t, ' ', \n) and returns the result as a vector of strings. Here's what I have so far:
std::vector<std ::string> tokenize (std::string foo) { std::istringstr eam s (foo); std::vector <std::string> v; std::string tok;
for (;;) // infinite loop { // try to extract token s >> tok;
// if string was read, push onto vector else break out of loop if (s.good()) v.push_back(tok ); else break; }
return v; }
The problem is that now if given a string that doesn't have whitespace at the end (ex - "this is a string"), the last token will be passed up because s.good() will return false not when the last extraction failed, but when there is no further input in the istringstream. If I restructure the loop as:
while (s.good()) { s >> tok; v.push_back(tok ); }
then if there is whitespace after the last token (ex - "this is a string "), then v will end up with the last token repeated, as an extraction when there is only whitespace in the istringstream will not modify the tok variable (so it keeps it's old value which was the last token successfully read, and then v pushes this onto the end of the vector).
Any suggestions?
Thanks,
Adam Parkin
while (s >> tok)
v.push_back(tok );
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christopher Benson-Manica |
last post by:
(if this is a FAQ, I apologize for not finding it)
I have a C-style string that I'd like to cleanly separate into tokens
(based on the '.' character) and then convert those tokens to unsigned
integers. What is the best standard(!) C++ way to accomplish this?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
|
by: Allan Bruce |
last post by:
I have a string like:
"FL:1234ABCD:3:FileName With Spaces.txt\n"
and I want to read the values separated by ':' into variables. I tried to
use sscanf like this:
sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName);
but the lGUID just continues until whitespace or \0 is found. I need a way
|
by: BGP |
last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow
Jones/NASDAQ/etc. stock prices as input, parse them, and do things with
it. The user can just cut and paste back prices into a window and hit
a button to process it.
The information thus enters the program as a char array. Prices can be
between $1 and $100, including cents. So we can have prices such as
3.01, 1.56, 11.57, etc. The char array is an alphanumeric...
|
by: Bit Byte |
last post by:
I want to parse a string (actually a date [always in the format
'YYYYMMDD') into integer values like this:
void parseDate(const string&sdate, int& day, int& mon, int &year)
{
istringstream iss(sdate) ;
....
}
I can do this easily in C (using atoi etc), but i wanted not too sure
|
by: Adrian |
last post by:
Hi All,
Is there anyway to change what isspace thinks is a space character.
I am parsing some log files and it would be nice to just read a field as what ever is between quotes or between 's ie clf log files
I know I can go char by char or find_last_of etc, but I would like to know if it is possible with locales and facets?
Also are there any suggestions for outputting in multiple languages? Can I use locales again?
I was just...
| |
by: Sudzzz |
last post by:
Hi,
I'm trying to convert a string something like this
"{201,23,240,56,23,45,34,23}" into an array in C++
Please help.
Thanks,
Sudzzz
|
by: Grey Alien |
last post by:
Does *ANYONE* in here know how I may parse the various date/time
'elements' from a string?. The input string has the ff format:
'YYYY-MM-DD HH:MM:SS AM'
|
by: arnuld |
last post by:
This works fine, I welcome any views/advices/coding-practices :)
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a program to store each line from a file into a
* vector<string>. Now, use istringstream to read read each line
* from the vector a word at a time.
|
by: tech |
last post by:
Hi, I need to parse a string used to represent a time and then
populate
a simple time struct. The time string will always be this format
23:45.45 ie hours separated from mins by ':' and minutes separated
from seconds by '.'
The string will be 8 chars in len. I've come up with some simple code
below
but am wondering do i really need a wstringstream and a string to do
this. Can
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |