473,218 Members | 1,465 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,218 software developers and data experts.

istringstream???

bml
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstream"

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 "istringstream"

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 "<<"?


Jul 22 '05 #1
3 4683
"bml" <le*****@yahoo.com> wrote...
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstream"

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 "istringstream"

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::stringstream',
which is bi-directional. Why have std::ostream and std::istream?..

V
Jul 22 '05 #2
bml
Victor Bazarov <v.********@comAcast.net> wrote in message
news:FInRb.161776$xy6.780699@attbi_s02...
"bml" <le*****@yahoo.com> wrote...
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstream"

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 "istringstream"

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::stringstream',
which is bi-directional. Why have std::ostream and std::istream?..

Exactly is it what I wanted to ask.

Jul 22 '05 #3
bml wrote:
2. Sub-string of an "istringstream"

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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
8
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...
7
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...
4
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...
4
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...
6
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...
8
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
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));
11
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++
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.