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 "<<"? 3 4694
"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
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.
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 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
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...
| |