What to use instead of sprintf?

Discussion in 'C++' started by cppaddict, Jun 15, 2004.

  1. cppaddict

    cppaddict Guest

    Hi,

    Currently, whenever I need to work with numbers as strings, I am using
    sprintf to do the conversion. This seems a little prehistoric. Is
    there a current STL alternative?

    One would think that std::string would support it somehow. But things
    like:

    std::string test = "hello";
    test += 99;
    std::cout << test;

    do not result in "test99" as I would like. Can anyone help me out, or
    is something in the printf family of functions still required?

    Thanks,
    cpp
    cppaddict, Jun 15, 2004
    #1
    1. Advertising

  2. cppaddict

    Nomak Guest

    Le 15/06/2004 à 01:16:49, cppaddict <> a écrit:

    > Hi,

    Hi

    > [...]


    $ cat ss.cc
    #include <iostream>
    #include <sstream>
    #include <string>

    using std::cout;
    using std::endl;
    using std::string;
    using std::stringstream;

    int
    main()
    {
    stringstream ss;

    string test = "hello";

    ss << test << 99;

    cout << ss.str() << endl;

    return 0;
    }
    $ g++ -W -Wall -std=c++98 ss.cc
    $ ./a.out
    hello99

    --
    Nomak
    Nomak, Jun 15, 2004
    #2
    1. Advertising

  3. cppaddict

    Phlip Guest

    Nothing personal, dude, but do you, like, own a C++ tutorial on paper??

    cppaddict wrote:

    > Currently, whenever I need to work with numbers as strings, I am using
    > sprintf to do the conversion. This seems a little prehistoric. Is
    > there a current STL alternative?
    >
    > One would think that std::string would support it somehow. But things
    > like:
    >
    > std::string test = "hello";
    > test += 99;
    > std::cout << test;


    #include <sstream>
    ....
    std::stringstream z;
    z << "hello " << 99;
    std::cout << z.str();

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Jun 15, 2004
    #3
  4. cppaddict

    cppaddict Guest


    >main()
    >{
    > stringstream ss;
    >
    > string test = "hello";
    >
    > ss << test << 99;
    >
    > cout << ss.str() << endl;
    >
    > return 0;
    >}


    Yes, sstream. I knew there was something.

    thank you Nomak and Philip.
    cppaddict, Jun 15, 2004
    #4
  5. cppaddict

    Default User Guest

    Default User, Jun 15, 2004
    #5
  6. cppaddict <> writes:

    > Hi,
    >
    > Currently, whenever I need to work with numbers as strings, I am using
    > sprintf to do the conversion. This seems a little prehistoric. Is
    > there a current STL alternative?
    >
    > One would think that std::string would support it somehow. But things
    > like:
    >
    > std::string test = "hello";
    > test += 99;
    > std::cout << test;
    >
    > do not result in "test99" as I would like. Can anyone help me out, or
    > is something in the printf family of functions still required?
    >
    > Thanks,
    > cpp


    Take a look at boost format library.

    http://boost.org/libs/format/index.html

    --
    Regards,
    Misha Polatov
    MetaCommunications Engineering
    Misha Polatov, Jun 17, 2004
    #6
    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. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,578
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,039
  3. CJ
    Replies:
    1
    Views:
    1,305
    Davlet Panech
    Oct 28, 2003
  4. Mike Chirico
    Replies:
    2
    Views:
    3,809
    Grumble
    Nov 19, 2003
  5. Pilatus
    Replies:
    3
    Views:
    530
    Pilatus
    Dec 18, 2003
Loading...

Share This Page