Could you help and answer my questions of istringstream? Thanks a lot!
1. Reuse an "istringstr eam"
istringstream ist;
ist.str("This is FIRST test string");
ist.str("This is SECOND test string");
cout << ist.str() << endl;
Is there any problem with the above code?
Is it necessary to add "ist.clear( );" between those two "ist.str()" ?
2. Sub-string of an "istringstr eam"
string str1, str2, str3, str4;
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;
How to insert the sub-string, "test", directly into str4, without going
through str1, str2, and str3?
How to remove the counterpart of "str1" from "ist" after "ist >> str1;",
"str1" from "ist" after "ist >> str1;" and so on?
3. Usage of skipws
istringstream ist;
ist.str("This is FIRST test string");
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;
ist >> skipws >> str1 ;
ist >> skipws >> str2 ;
ist >> skipws >> str3;
ist >> skipws >> str4;
Adding "skipws" don't change format and content of str1,...str4 at all? So
what's the usage of "skipws"?
4. Why need ostringstream and istringstream separate, instead of combining
them into a single one with both ">>" and "<<"? 3 4742
"bml" <le*****@yahoo. com> wrote... Could you help and answer my questions of istringstream? Thanks a lot!
1. Reuse an "istringstr eam"
istringstream ist; ist.str("This is FIRST test string");
ist.str("This is SECOND test string"); cout << ist.str() << endl;
Is there any problem with the above code?
Aside from it being a fragment outside of any function, no.
Is it necessary to add "ist.clear( );" between those two "ist.str()" ?
You mean, between those two "ist.str( <some string literal> )"? No.
2. Sub-string of an "istringstr eam"
string str1, str2, str3, str4; ist >> str1 ; ist >> str2 ; ist >> str3; ist >> str4;
How to insert the sub-string, "test", directly into str4, without going through str1, str2, and str3?
There is no way. It's a stream. Well, not entirely true, you could
of course, position the sream if you knew where.
How to remove the counterpart of "str1" from "ist" after "ist >> str1;", "str1" from "ist" after "ist >> str1;" and so on?
What do you mean by "remove"? The stream [buffer] position is changed
after every reading. Why do you need to remove anything?
3. Usage of skipws
istringstream ist; ist.str("This is FIRST test string"); ist >> str1 ; ist >> str2 ; ist >> str3; ist >> str4;
ist >> skipws >> str1 ; ist >> skipws >> str2 ; ist >> skipws >> str3; ist >> skipws >> str4;
Adding "skipws" don't change format and content of str1,...str4 at all? So what's the usage of "skipws"?
IIRC, it affects only some operations. For example, if you read 'str1' and
then try to extract the _rest_ of the buffer, youi will get "is FIRST..."
instead of " is FIRST...".
4. Why need ostringstream and istringstream separate, instead of combining them into a single one with both ">>" and "<<"?
I guess I don't understand the question. There is 'std::stringstr eam',
which is bi-directional. Why have std::ostream and std::istream?..
V
Victor Bazarov <v.********@com Acast.net> wrote in message
news:FInRb.1617 76$xy6.780699@a ttbi_s02... "bml" <le*****@yahoo. com> wrote... Could you help and answer my questions of istringstream? Thanks a lot!
1. Reuse an "istringstr eam"
istringstream ist; ist.str("This is FIRST test string");
ist.str("This is SECOND test string"); cout << ist.str() << endl;
Is there any problem with the above code? Aside from it being a fragment outside of any function, no.
Is it necessary to add "ist.clear( );" between those two "ist.str()" ?
Thank you for your answers! So what's the usage of ist.clear()?
You mean, between those two "ist.str( <some string literal> )"? No.
2. Sub-string of an "istringstr eam"
string str1, str2, str3, str4; ist >> str1 ; ist >> str2 ; ist >> str3; ist >> str4;
How to insert the sub-string, "test", directly into str4, without going through str1, str2, and str3?
There is no way. It's a stream. Well, not entirely true, you could of course, position the sream if you knew where.
What do you mean by positioning the stream? 4. Why need ostringstream and istringstream separate, instead of
combining them into a single one with both ">>" and "<<"?
I guess I don't understand the question. There is 'std::stringstr eam', which is bi-directional. Why have std::ostream and std::istream?..
Exactly is it what I wanted to ask.
bml wrote: 2. Sub-string of an "istringstr eam"
string str1, str2, str3, str4; ist >> str1 ; ist >> str2 ; ist >> str3; ist >> str4;
How to insert the sub-string, "test", directly into str4, without going through str1, str2, and str3?
There is no way. It's a stream. Well, not entirely true, you could of course, position the sream if you knew where.
What do you mean by positioning the stream?
How old are you? I don't ask because I want to be impolite
or I want to insult you BUT I just want to know if you are old
enough to remember the days of audio casette recorders or tape
drives.
A stream works like a tape. You can only read and write sequentially.
Well. If you know exactly what you are looking for you can press the button on your tape deck and do a fast forward. But you need to
be carefull, without a tape meter it is practically impossible to
position the tape *exactly* where you want to start. And even then it
is impossible if you don't know the length of each song on the tape.
With a stream its similar: If you know the exact location you can
position the stream there and start reading. But without this knowledge
you are lost. So if there are 4 words in the stream and you do not know
anything about them such as the length of each, there simply is no way to
determine the start of the 4-th word other then read through the previous
words.
--
Karl Heinz Buchegger kb******@gascad .at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Samuele Armondi |
last post by:
Hi everyone,
Since istringstream objects are not assignable, I'm using the following code
to allocate some dynamically. My question is: Is this the correct way of
doing it? Am I deleting all the allocated memory correctly? Or am I missing
something glaringly simple?
Thanks in advance,
S. Armondi
std::istringstream** ArgStream;
|
by: Agent Mulder |
last post by:
I try to remove the spaces from a string
using an old trick that involves an
istringstream object. I expect the while-condition
while(istringstream>>string) to evaluate to
false once the istringstream is exhausted, but
that is not the case. What am I missing?
#include<iostream>
#include<sstream>
#include<string>
|
by: Luther Baker |
last post by:
Hi,
My question is regarding std::istringstream. I am serializing data to
an ostringstream and the resulting buffer turns out just fine.
But, when I try the reverse, when the istringstream encounters the two
byte shorts, it either thinks it has reached the null terminator? or
eof and consequently stops reading values back in. It doesn't matter
whether or not I use the std::ios::binary flag when opening the
istringstream or the...
|
by: Brandon |
last post by:
I'm using an istringstream to convert some integers stored in string
form to integers. However, each integer is stored in a differnt
string. So, I used istringstream's str(string) method for the first
string and then used the >> operator to extract the integer. It
worked just fine. So, on to the second string. I used str(string)
again but this time extraction gave me a seemingly random number
(-858993460). Once an istringstream is...
|
by: dinks |
last post by:
Hi I'm really new to c++ so please forgive me if this is really basic
but im stuck... I am trying to make a data class that uses
istringstram and overloaded << and >> operators to input and output
data. The data comes in string lines like "OREBlegQ 14854 731.818"
which need to be split into a string, int and double when stored in
the class. Can anyone help? This is what i have so far:
/* Begin Code */
#include <sstream>
| |
by: JustSomeGuy |
last post by:
I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
ie when the function returns I want the istringstream to be
unmodified...
However when I try to pass it
fn(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.
|
by: Randy Yates |
last post by:
Why does this:
string AWord(string& line)
{
return line;
}
bool MYOBJECT::MyFunction(string &line)
{
int day;
|
by: James Aguilar |
last post by:
Hello all,
I am trying to use an istringstream to do some input off of cin by lines.
The following snippet does not work:
char buf;
cin.getline(buf, 90);
istringstream line1(string(buf));
|
by: icanoop |
last post by:
I would like to do this
MyClass x;
istringstream("XXX") >> x; // Works in VC++ but not GCC
instead of
MyClass x;
istringstream iss("XXX");
iss >> x; // Works in both GCC and VC++
|
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: 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: 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: 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: 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...
| |