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

static istringstream

Hello everyone!

My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So I wrote simple test function. But it doesn't work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>

int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;

istr.str(str);

istr >val;
std::cout << "Number: " << val << std::endl;
}

return 0;
}
Jun 27 '08 #1
12 2421
On Jun 7, 5:36*pm, sergey.lukosh...@gmail.com wrote:
My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So *I wrote simple test function. But it doesn't work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>

int main()
{
* * *static std::string str;
* * *static std::istringstream istr;
* * *static unsigned val;

* * while( true )
* * {
* * * * std::cout << "Enter num: " << std::endl;
* * * * std::cin >str;

* * * * istr.str(str);

* * * * istr >val;
* * * * std::cout << "Number: " << val << std::endl;
* * }

* * return 0;
}
You need to clear the buffer held by the stringstream object. Make a
call istr.str("");

Alternatively, why don't you use boost::lexical_cast<which is just a
header only library from boost? Saves you from error handling routines
as well which otherwise you need to employ yourself for cases where it
fails.
Jun 27 '08 #2
On Jun 7, 7:51*pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Jun 7, 5:36*pm, sergey.lukosh...@gmail.com wrote:
My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So *I wrote simple test function. But it doesn't work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>
int main()
{
* * *static std::string str;
* * *static std::istringstream istr;
* * *static unsigned val;
* * while( true )
* * {
* * * * std::cout << "Enter num: " << std::endl;
* * * * std::cin >str;
* * * * istr.str(str);
* * * * istr >val;
* * * * std::cout << "Number: " << val << std::endl;
* * }
* * return 0;
}

You need to clear the buffer held by the stringstream object. Make a
call istr.str("");
Sorry, correction : it seems eof() bit is being set for the
stringstream. And hence it further doesn't work. You need to clear
that flag for the next iteration. For that, you need to call the
clear() member. Verified that by putting in:

std::cout << std::endl << istr.eof() << " " << istr.fail() << " "
<< istr.bad() << std::endl;

You need to check those flags, eof() may be ok but if the rest of the
2 bits are set then that would probably mean an error with the IO
operation.
Alternatively, why don't you use boost::lexical_cast<which is just a
header only library from boost? Saves you from error handling routines
as well which otherwise you need to employ yourself for cases where it
fails.
Jun 27 '08 #3
On Jun 7, 8:19 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Jun 7, 7:51 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Jun 7, 5:36 pm, sergey.lukosh...@gmail.com wrote:
My task is in converting numbers from string to int variables. I used
istringstream to perform it.
So I wrote simple test function. But it doesn't work as I expected
because val is not being changed while the program is running. Could
you please tell me why ??
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;
while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;
istr.str(str);
istr >val;
std::cout << "Number: " << val << std::endl;
}
return 0;
}
You need to clear the buffer held by the stringstream object. Make a
call istr.str("");

Sorry, correction : it seems eof() bit is being set for the
stringstream. And hence it further doesn't work. You need to clear
that flag for the next iteration. For that, you need to call the
clear() member. Verified that by putting in:

std::cout << std::endl << istr.eof() << " " << istr.fail() << " "
<< istr.bad() << std::endl;

You need to check those flags, eof() may be ok but if the rest of the
2 bits are set then that would probably mean an error with the IO
operation.
Alternatively, why don't you use boost::lexical_cast<which is just a
header only library from boost? Saves you from error handling routines
as well which otherwise you need to employ yourself for cases where it
fails.

Thanks a lot ! Now I call istr.clear() and it works properly.
Here my fixed code.

P.S I've never used boost lib.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;

istr.clear();
istr.str(str);

istr >val;
std::cout << "Number: " << val << std::endl;
}

return 0;

- Hide quoted text -
- Show quoted text -
}

Jun 27 '08 #4
se**************@gmail.com kirjutas:

[...]
>
Thanks a lot ! Now I call istr.clear() and it works properly.
Here my fixed code.

P.S I've never used boost lib.
You should, lots of it will be included in the next C++ standard.
>
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;
main() is called only once so the keyword 'static' does not change
anything here. Why do you think 'static' is needed?

And why do you define them here, not inside the loop where they are used?
Would have avoided all the trouble in the first place...
while( true )
{
std::cout << "Enter num: " << std::endl;
std::cin >str;

istr.clear();
istr.str(str);

istr >val;
std::cout << "Number: " << val << std::endl;
}

return 0;

- Hide quoted text -
- Show quoted text -
}
Regards
Paavo
Jun 27 '08 #5
On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.eewrote:
You should, lots of it will be included in the next C++ standard.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

main() is called only once so the keyword 'static' does not change
anything here. Why do you think 'static' is needed?

And why do you define them here, not inside the loop where they are used?
Would have avoided all the trouble in the first place...
Well, I agree with you. In this case there is no benefit of using
static keyword. But if I take the contents of main() and put it in my
function that I'm going to call many times at a runtime it will reduce
the cost of function calling.

Jun 27 '08 #6
se**************@gmail.com kirjutas:
On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.eewrote:
>You should, lots of it will be included in the next C++ standard.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

main() is called only once so the keyword 'static' does not change
anything here. Why do you think 'static' is needed?

And why do you define them here, not inside the loop where they are
used? Would have avoided all the trouble in the first place...

Well, I agree with you. In this case there is no benefit of using
static keyword. But if I take the contents of main() and put it in my
function that I'm going to call many times at a runtime it will reduce
the cost of function calling.
Are you sure? Have you measured this? If the variables are not local, the
compiler has harder times to optimize the code. If the variables are
local, the compiler can in principle optimize they all away. In your
current code you need an extra call to clear() function as well, which
would not be needed in case of local variables.

Also, by using such static variables you make your function non-reentrant
and thus hard and slow (external locking needed) to use in multithreaded
programs, with no apparent reasons for that.

IOW, the sooner you wander away from the perils of premature optimization
the better! For deciding what would be the best optimization you need to
be an expert, and even then you have to measure your hypothesis. Here I
think a possible optimization could be to use a certain C function
instead of multiple C++ objects construction, but I'm not sure at all.

Regards
Paavo

Jun 27 '08 #7
se**************@gmail.com wrote:
On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.eewrote:
>You should, lots of it will be included in the next C++ standard.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;

main() is called only once so the keyword 'static' does not change
anything here. Why do you think 'static' is needed?

And why do you define them here, not inside the loop where they are used?
Would have avoided all the trouble in the first place...

Well, I agree with you. In this case there is no benefit of using
static keyword. But if I take the contents of main() and put it in my
function that I'm going to call many times at a runtime it will reduce
the cost of function calling.
a) You should measure that.

b) This move will make your function unsuitable for multi-threaded client
code. You should definitely document that it is not reentrant.
Best

Kai-Uwe Bux
Jun 27 '08 #8
On Jun 7, 10:15 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
sergey.lukosh...@gmail.com wrote:
On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.eewrote:
You should, lots of it will be included in the next C++ standard.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;
main() is called only once so the keyword 'static' does not change
anything here. Why do you think 'static' is needed?
And why do you define them here, not inside the loop where they are used?
Would have avoided all the trouble in the first place...
Well, I agree with you. In this case there is no benefit of using
static keyword. But if I take the contents of main() and put it in my
function that I'm going to call many times at a runtime it will reduce
the cost of function calling.

a) You should measure that.

b) This move will make your function unsuitable for multi-threaded client
code. You should definitely document that it is not reentrant.

Best

Kai-Uwe Bux
Words of wisdom.....

But all in all I have to call istr.clear(). Because the specific of my
function is to process string in cycle. Thanks for answers!!

Jun 27 '08 #9
IOW, the sooner you wander away from the perils of premature optimization
the better! For deciding what would be the best optimization you need to
be an expert, and even then you have to measure your hypothesis. Here I
think a possible optimization could be to use a certain C function
instead of multiple C++ objects construction, but I'm not sure at all.
Ok. Imagine I got a string representing IPv4 address "1ABC2DF1" and I
should convert it to classical view "26.188.45.241". It seems to me
that the best way to to this is using stringstream...
Jun 27 '08 #10
se**************@gmail.com kirjutas:
>
>IOW, the sooner you wander away from the perils of premature
optimization the better! For deciding what would be the best
optimization you need to be an expert, and even then you have to
measure your hypothesis. Here I think a possible optimization could
be to use a certain C function instead of multiple C++ objects
construction, but I'm not sure at all.

Ok. Imagine I got a string representing IPv4 address "1ABC2DF1" and I
should convert it to classical view "26.188.45.241". It seems to me
that the best way to to this is using stringstream...
Yes, why not? There is nothing wrong with stringstream. I objected only to
using static objects, and reusing the same objects without any sound
reason.

Cheers
Paavo
Jun 27 '08 #11
On Jun 8, 12:17 am, Paavo Helde <nob...@ebi.eewrote:
sergey.lukosh...@gmail.com kirjutas:
IOW, the sooner you wander away from the perils of premature
optimization the better! For deciding what would be the best
optimization you need to be an expert, and even then you have to
measure your hypothesis. Here I think a possible optimization could
be to use a certain C function instead of multiple C++ objects
construction, but I'm not sure at all.
Ok. Imagine I got a string representing IPv4 address "1ABC2DF1" and I
should convert it to classical view "26.188.45.241". It seems to me
that the best way to to this is using stringstream...

Yes, why not? There is nothing wrong with stringstream. I objected only to
using static objects, and reusing the same objects without any sound
reason.

Cheers
Paavo
Thank you!
Jun 27 '08 #12
On Jun 7, 7:45 pm, sergey.lukosh...@gmail.com wrote:
On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.eewrote:
You should, lots of it will be included in the next C++ standard.
int main()
{
static std::string str;
static std::istringstream istr;
static unsigned val;
main() is called only once so the keyword 'static' does not
change anything here. Why do you think 'static' is needed?
And why do you define them here, not inside the loop where
they are used? Would have avoided all the trouble in the
first place...
Well, I agree with you. In this case there is no benefit of
using static keyword. But if I take the contents of main() and
put it in my function that I'm going to call many times at a
runtime it will reduce the cost of function calling.
On the other hand, it will work correctly and be maintainable.

And who says it will reduce the cost of calling the function.
Until you have actual measurements indicating that this is
causing a real performance problem, it's just plain stupid to
add complexity for nothing. If you want a new, clean object,
you construct one.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #13

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...
3
by: bml | last post by:
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...
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: 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++
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
agi2029
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,...

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.