atoi: stringstream or old C sprintf function

Discussion in 'C++' started by Mike Chirico, Nov 19, 2003.

  1. Mike Chirico

    Mike Chirico Guest

    Sorry about the newbie question. What's the best way to convert
    numbers to strings in C++. The following works, but is it better than
    using the sprintf() "old C" way of converting?


    #include <iostream>
    #include <string>
    #include <sstream>
    #include <list>

    using namespace std;
    int main(void)
    {
    list<string> ml;
    string s;
    stringstream ss;



    for(int i=0; i < 3000; ++i)
    {
    ss << "sample text " << i ;
    ml.push_front( ss.str() );
    ss.str("");
    }

    cout << ml.front() << endl;
    }


    What about the fact that the stringstream has to be cleared
    ss.str("")...does that incur additional overhead? Maybe there is a
    better way?


    Regards,


    Mike Chirico
     
    Mike Chirico, Nov 19, 2003
    #1
    1. Advertising

  2. Re: stringstream or old C sprintf function

    > Sorry about the newbie question. What's the best way to convert
    > numbers to strings in C++. The following works, but is it better than
    > using the sprintf() "old C" way of converting?
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <sstream>
    > #include <list>
    >
    > using namespace std;
    > int main(void)
    > {
    > list<string> ml;
    > string s;
    > stringstream ss;
    >
    > for(int i=0; i < 3000; ++i)
    > {
    > ss << "sample text " << i ;
    > ml.push_front( ss.str() );
    > ss.str("");
    > }
    >
    > cout << ml.front() << endl;
    > }
    >
    >
    > What about the fact that the stringstream has to be cleared
    > ss.str("")...does that incur additional overhead? Maybe there is a
    > better way?


    Is your program too slow? If yes, is it caused by stringstream? If the
    answer is no to either of these questions, then why worry about the
    overhead? Unless the stringstream introduces an unacceptable perfomance
    problem in your program, I'd say stick with what you have got; it looks
    pretty good to me!

    Since ss is only used inside the loop, you might consider moving the
    "stringstream ss;" line inside loop and remove the "ss.str("");" line.
    It is a little bit cleaner, though it is probably also slightly slower
    (but I doubt you'd be able to measure the difference).

    The std::stringstream solution is not necessarilly the fastest solution,
    but it is clean and safe unlike the C way of converting. The overhead of
    ss.str("") is probably very small compared to the cost of doing
    ml.push_front(ss.str());.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Nov 19, 2003
    #2
    1. Advertising

  3. Mike Chirico

    Grumble Guest

    Grumble, Nov 19, 2003
    #3
    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. lonelyplanet999
    Replies:
    8
    Views:
    44,519
  2. mark
    Replies:
    2
    Views:
    18,392
    Victor Bazarov
    Jun 14, 2004
  3. Sonia

    atoi function

    Sonia, Feb 12, 2005, in forum: C++
    Replies:
    8
    Views:
    22,241
    Sonia
    Feb 13, 2005
  4. Allan Bruce

    Macro for atoi similar function

    Allan Bruce, Jul 4, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,166
    Allan Bruce
    Jul 5, 2003
  5. Bansidhar

    atoi function

    Bansidhar, Jan 30, 2004, in forum: C Programming
    Replies:
    5
    Views:
    7,262
    Peter Pichler
    Jan 30, 2004
Loading...

Share This Page