PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > static istringstream
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
static istringstream

Réponse
 
LinkBack Outils de la discussion
Vieux 07/06/2008, 13h36   #1
sergey.lukoshkin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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;
}
  Réponse avec citation
Vieux 07/06/2008, 15h51   #2
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

On Jun 7, 5:36pm, 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.
  Réponse avec citation
Vieux 07/06/2008, 17h19   #3
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

On Jun 7, 7:51pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
> On Jun 7, 5:36pm, 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.

  Réponse avec citation
Vieux 07/06/2008, 17h35   #4
sergey.lukoshkin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

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 -
}

  Réponse avec citation
Vieux 07/06/2008, 18h27   #5
Paavo Helde
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

sergey.lukoshkin@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
  Réponse avec citation
Vieux 07/06/2008, 18h45   #6
sergey.lukoshkin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.ee> wrote:

> 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.

  Réponse avec citation
Vieux 07/06/2008, 19h14   #7
Paavo Helde
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

sergey.lukoshkin@gmail.com kirjutas:

> On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.ee> wrote:
>
>> 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

  Réponse avec citation
Vieux 07/06/2008, 19h15   #8
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: static istringstream

sergey.lukoshkin@gmail.com wrote:

> On Jun 7, 9:27 pm, Paavo Helde <nob...@ebi.ee> wrote:
>
>> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h37.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15363 seconds with 16 queries