static istringstream

Discussion in 'C++' started by sergey.lukoshkin@gmail.com, Jun 7, 2008.

  1. Guest

    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 7, 2008
    #1
    1. Advertising

  2. On Jun 7, 5:36 pm, 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.
     
    Abhishek Padmanabh, Jun 7, 2008
    #2
    1. Advertising

  3. On Jun 7, 7:51 pm, Abhishek Padmanabh <>
    wrote:
    > On Jun 7, 5:36 pm, 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.
     
    Abhishek Padmanabh, Jun 7, 2008
    #3
  4. Guest

    On Jun 7, 8:19 pm, Abhishek Padmanabh <>
    wrote:
    > On Jun 7, 7:51 pm, Abhishek Padmanabh <>
    > wrote:
    >
    >
    >
    > > On Jun 7, 5:36 pm, 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 7, 2008
    #4
  5. Guest

    On Jun 7, 9:27 pm, Paavo Helde <> 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.
     
    , Jun 7, 2008
    #5
  6. Kai-Uwe Bux Guest

    wrote:

    > On Jun 7, 9:27 pm, Paavo Helde <> 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
     
    Kai-Uwe Bux, Jun 7, 2008
    #6
  7. Guest

    On Jun 7, 10:15 pm, Kai-Uwe Bux <> wrote:
    > wrote:
    > > On Jun 7, 9:27 pm, Paavo Helde <> 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


    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 7, 2008
    #7
  8. Guest


    > 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 7, 2008
    #8
  9. Guest

    On Jun 8, 12:17 am, Paavo Helde <> wrote:
    > 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 7, 2008
    #9
  10. James Kanze Guest

    On Jun 7, 7:45 pm, wrote:
    > On Jun 7, 9:27 pm, Paavo Helde <> 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.


    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:
    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
     
    James Kanze, Jun 8, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuele Armondi

    Allocating istringstream objects

    Samuele Armondi, Jul 11, 2003, in forum: C++
    Replies:
    1
    Views:
    367
    Samuele Armondi
    Jul 11, 2003
  2. Agent Mulder

    istringstream to bool

    Agent Mulder, Aug 23, 2003, in forum: C++
    Replies:
    8
    Views:
    1,012
    Agent Mulder
    Aug 24, 2003
  3. Viet Le Hong

    istringstream question

    Viet Le Hong, Sep 18, 2003, in forum: C++
    Replies:
    3
    Views:
    666
    Buster Copley
    Sep 18, 2003
  4. bml

    istringstream???

    bml, Jan 27, 2004, in forum: C++
    Replies:
    3
    Views:
    1,052
    Karl Heinz Buchegger
    Jan 27, 2004
  5. Donald Canton

    istringstream syntax error question

    Donald Canton, Jan 29, 2004, in forum: C++
    Replies:
    1
    Views:
    407
    David Harmon
    Jan 30, 2004
Loading...

Share This Page