Hi,
I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.
Thanks for suggestion,
-Mathieu
(*)
#include <iostream>
#include <sstream>
int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstr eam is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;
return 0;
} 4 8284
mathieu wrote:
Hi,
I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.
Thanks for suggestion,
-Mathieu
(*)
#include <iostream>
#include <sstream>
int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstr eam is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;
return 0;
}
'std::ws' does not set some kind of "from now on consider whitespace
not whitespace any longer" flag. No such flag exists. 'std::ws' just
skips all whitespace from current position to the next non-whitespace
character.
What you need is "get the rest of the stream into my 'string'" action.
Instead of 'is >st', use 'getline':
is >std::ws;
std::getline(is , st); // not the shift.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
mathieu wrote:
Hi,
I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.
istream >string
Reads until it finds whitespace. Unfortunately, the space between the words
is whitespace. You could alter this behaviour by changing the locale, but
the easiest solution is to use the getline function.
#include <iostream>
#include <sstream>
#include <string>
#include <istream>
Formally, you need these two headers. Though most implementations of
<sstreamwill pull them in, you are not guaranteed this.
int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstr eam is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
Change this last line to
std::getline(is , st);
std::cout << a << " " << b << " " << c << " " << st << std::endl;
return 0;
}
--
rbh
On 2007-07-13 16:31, mathieu wrote:
Hi,
I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.
Thanks for suggestion,
-Mathieu
(*)
#include <iostream>
#include <sstream>
int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstr eam is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;
return 0;
}
No, std::ws will only extract the whitespace between the '3' and the
'Hello', but since you have not done 'is >std::noskipw s' it will be
skipped anyway (so 'is >std::ws' will have no effect). I'm not sure
how to solve your problem but it might be possible to change the
delimiting characters somehow. Or if you know that the string will be at
the end use is.str().
--
Erik Wikström
Erik Wikström <Er***********@ telia.comwrote in message...
On 2007-07-13 16:31, mathieu wrote:
Hi,
#include <iostream>
#include <sstream>
int main(int, char *[]){
// or just: 'int main(){}'
const char s[] = "1 2 3 Hello World";
std::istringstr eam is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;
return 0;
}
No, std::ws will only extract the whitespace between the '3' and the
'Hello', but since you have not done 'is >std::noskipw s' it will be
skipped anyway (so 'is >std::ws' will have no effect). I'm not sure
how to solve your problem but it might be possible to change the
delimiting characters somehow.
Or if you know that the string will be at the end use is.str().
Oh?
// ....
is >a >b >c;
is >std::ws; // could be done with 'is.ignore( 1 );'
// is >st;
st = is.str();
std::cout << a << " " << b << " " << c << " " << st << std::endl;
// out:1 2 3 1 2 3 Hello World
std::getline( is, st );
std::cout << a << " " << b << " " << c << " " << st << std::endl;
// out:1 2 3 Hello World
// ....
OP: If you only wanted to extract 'Hello' using std::getline(), you can set
the delimiter ('\n' is default).
// is >st;
std::getline( is, st, ' ' ); // note: that's a single char, not " "
(string).
// (should) out:1 2 3 Hello
--
Bob R
POVrookie This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jason Heyes |
last post by:
To my understanding, std::vector does not use reference counting to avoid
the overhead of copying and initialisation. Where can I get a reference
counted implementation of std::vector? Thanks.
|
by: William Payne |
last post by:
Hello, when I was writing a user-driven test program for a data structure I
wrote, I encountered an annoying problem. The test program is laid out as a
menu with several numbered options. The user selects one option by typing in
its corresponding number (int). Depending on the choice he made, he may be
asked to provide another integer (or no input at all). So all the user does
is entering integers. I don't want the user to be able to cause...
|
by: Abhishek |
last post by:
Hi,
I am trying to use the STL for C++ on Compaq. From what I read in
the man pages and several online documentation, it is recommended that
in order to use the STL on COMPAQ , I need to use the following flag -
__USE_STD_IOSTREAM. The compilation goes through fine, but at the
linking time, I get the following errors -
Unresolved:
std::basic_istream<char, std::char_traits<char> >::getline(char*,
long, char)
std::basic_ifstream<char,...
|
by: Don Wash |
last post by:
Hi All!
I've been searching everywhere for a simple sample of producing a bar graph
using CrystalReport by specifying SQL Query, and I've found none of it! I
find so many complex samples with so many features buried in the sample but
all I want to know is a very simple thing, how to create bar charts with
CrystalReport in VS.NET using SQL Query.
Could anyone provide me a sample showing how to produce a simple bar chart
using SQL Query...
|
by: alopatenko |
last post by:
I have a template class
template <Class W>
class WS
At some point I have to use a STL list WS<W>objects
so, I define
#include <list>
| |
by: Jeffrey Walton |
last post by:
Hi All,
I've done a little homework (I've read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup's Appendix D recommendation won't
compile in Microsoft VC++ 6.0).
My question is in reference to MultiByte Character Sets. Will this code
perform as expected? I understand every problem has a simple and
elegant solution that is wrong.
|
by: Basil |
last post by:
Hello!
Could You help me?
Where can I get samples with using of WS-AtomicTransaction and WS-ReliableMessaging?
Thanks, Basil.
|
by: frohlinger |
last post by:
Hi,
I need to perform some numeric calculations on a numeric float value,
that is received as wstring.
I would like to perform a check before converting the wstring to
float, checking that indeed the wstring contains a numeric value.
This is the actual conversion:
double fValue = _wtof(strValue.c_str());
if strValue contains some characters (e.g.: 'aaa'), _wtof returns 0,
therefore I do not really know that the input was initially...
|
by: levent |
last post by:
Hi All,
Consider the following block of code:
std::istringstream in( "1" ); // no space after `1'
int i;
in >i >std::ws;
assert( !in::fail() )
Does the standard have any mention whether assertion fails or not?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |