std::string and integers

Discussion in 'C++' started by Glen Able, Jul 8, 2004.

  1. Glen Able

    Glen Able Guest

    Obviously the following doesn't work:

    int i = 5;
    std::string myString = "Number is " + i + " thankyou please";

    So can anyone give me some idea what's the nicest way to this sort of thing?
    I'm sure it's not using sprintf.

    with thanks,
    G.A.
     
    Glen Able, Jul 8, 2004
    #1
    1. Advertising

  2. Glen Able

    Tim Love Guest

    "Glen Able" <> writes:

    >Obviously the following doesn't work:


    >int i = 5;
    >std::string myString = "Number is " + i + " thankyou please";


    >So can anyone give me some idea what's the nicest way to this sort of thing?
    >I'm sure it's not using sprintf.


    You can use cout-style formatting to print to a string. See the FAQ
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-38.1
     
    Tim Love, Jul 8, 2004
    #2
    1. Advertising

  3. Glen Able

    Gwar Guest

    On Thu, 8 Jul 2004, Glen Able wrote:

    > Obviously the following doesn't work:
    >
    > int i = 5;
    > std::string myString = "Number is " + i + " thankyou please";
    >
    > So can anyone give me some idea what's the nicest way to this sort of thing?
    > I'm sure it's not using sprintf.
    >
    > with thanks,
    > G.A.
    >
    >
    >

    // one way:

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


    using namespace std;

    int main()
    {
    int i = 5;
    string myString1 = "The number is ";
    string myString2 = " thank you, please";

    ostringstream os;

    os << myString1 << i << myString2;
    string msgString = os.str();

    cout << msgString << endl;

    }
     
    Gwar, Jul 8, 2004
    #3
  4. try
    int i = 5;
    std::string myString;
    myString = "Number is " + i + " thankyou please";
    // ANders


    On Thu, 08 Jul 2004 09:28:58 +0100, Glen Able wrote:

    > Obviously the following doesn't work:
    >
    > int i = 5;
    > std::string myString = "Number is " + i + " thankyou please";
    >
    > So can anyone give me some idea what's the nicest way to this sort of thing?
    > I'm sure it's not using sprintf.
    >
    > with thanks,
    > G.A.
     
    Anders Persson, Jul 8, 2004
    #4
  5. Glen Able

    Rolf Magnus Guest

    Anders Persson wrote:

    > try
    > int i = 5;
    > std::string myString;
    > myString = "Number is " + i + " thankyou please";
    > // ANders


    This won't compile, since it's illegal in C++ to add two pointers
    together.

    myString = "Number is " + i;

    would compile, but it would for sure not do what was intended.
     
    Rolf Magnus, Jul 8, 2004
    #5
  6. Glen Able

    Glen Able Guest

    "Gwar" <> wrote in message
    news:...
    >
    >> > Obviously the following doesn't work:

    > >
    > > int i = 5;
    > > std::string myString = "Number is " + i + " thankyou please";

    >


    > int i = 5;
    > string myString1 = "The number is ";
    > string myString2 = " thank you, please";
    >
    > ostringstream os;
    >
    > os << myString1 << i << myString2;
    > string msgString = os.str();
    >
    > cout << msgString << endl;


    Eek! Why isn't there anything nice, like a std::string ctor that takes an
    int, so I could then do
    "blah " + std::string(i) + " blah";
     
    Glen Able, Jul 8, 2004
    #6
  7. Glen Able

    Rolf Magnus Guest

    Glen Able wrote:

    > Eek! Why isn't there anything nice, like a std::string ctor that
    > takes an int,


    There is. It will create a string of the specified size.
     
    Rolf Magnus, Jul 8, 2004
    #7
  8. Glen Able

    Evan Carew Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Glen Able wrote:
    > "Gwar" <> wrote in message
    > news:...
    >
    >>>>Obviously the following doesn't work:
    >>>
    >>>int i = 5;
    >>>std::string myString = "Number is " + i + " thankyou please";

    >>

    >
    >> int i = 5;
    >> string myString1 = "The number is ";
    >> string myString2 = " thank you, please";
    >>
    >> ostringstream os;
    >>
    >> os << myString1 << i << myString2;
    >> string msgString = os.str();
    >>
    >> cout << msgString << endl;

    >
    >
    > Eek! Why isn't there anything nice, like a std::string ctor that takes an
    > int, so I could then do
    > "blah " + std::string(i) + " blah";
    >

    There is, its called lexical cast & is part of the Boost library. look
    at http://boost.org/libs/conversion/lexical_cast.htm

    The code looks like the following:

    string mystring;
    mystring = "The number is " + lexical_cast<short>(5) + " thank you, please";

    Evan

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFA7T4Zoo/Prlj9GScRAi+wAJ9R3yYSkpBSmORTEoGarWyCvTxAhQCeNm0L
    trw57xPoS3zrpilJjmpQc28=
    =e9Og
    -----END PGP SIGNATURE-----
     
    Evan Carew, Jul 8, 2004
    #8
  9. > There is, its called lexical cast & is part of the Boost library. look
    > at http://boost.org/libs/conversion/lexical_cast.htm
    >
    > The code looks like the following:
    >
    > string mystring;
    > mystring = "The number is " + lexical_cast<short>(5) + " thank you,

    please";
    >


    Errm

    lexical_cast<string>(5)

    john
     
    John Harrison, Jul 8, 2004
    #9
  10. "Glen Able" <> wrote in message
    news:ccjbi2$dua$1$...
    >
    > "Gwar" <> wrote in message
    > news:...
    > >
    > >> > Obviously the following doesn't work:
    > > >
    > > > int i = 5;
    > > > std::string myString = "Number is " + i + " thankyou please";

    > >

    >
    > > int i = 5;
    > > string myString1 = "The number is ";
    > > string myString2 = " thank you, please";
    > >
    > > ostringstream os;
    > >
    > > os << myString1 << i << myString2;
    > > string msgString = os.str();
    > >
    > > cout << msgString << endl;

    >
    > Eek! Why isn't there anything nice, like a std::string ctor that takes an
    > int, so I could then do
    > "blah " + std::string(i) + " blah";
    >


    Why would that be a good idea exactly?

    If you want an operator to concatenate strings with integers there is
    nothing stopping you writing it.

    string operator+(const string& lhs, int rhs)
    {
    ...
    }

    string operator+(int lhs, const string& rhs)
    {
    ...
    }

    Don't blame me if the results don't match your expectations though.

    john
     
    John Harrison, Jul 8, 2004
    #10
  11. Glen Able

    Glen Able Guest

    > If you want an operator to concatenate strings with integers there is
    > nothing stopping you writing it.
    >
    > string operator+(const string& lhs, int rhs)
    > {
    > ...
    > }


    Ooh, didn't know you could do global operator overloads, ta.
    In my excitement I just tried defining float operator+(float f1, float f2),
    but the compiler's on to me :)

    > Don't blame me if the results don't match your expectations though.
    >
    > john


    Do you have some specific problem in mind, John?

    ta,
    G.A.
     
    Glen Able, Jul 8, 2004
    #11
  12. Glen Able

    Rolf Magnus Guest

    Glen Able wrote:

    >> If you want an operator to concatenate strings with integers there is
    >> nothing stopping you writing it.
    >>
    >> string operator+(const string& lhs, int rhs)
    >> {
    >> ...
    >> }

    >
    > Ooh, didn't know you could do global operator overloads, ta.
    > In my excitement I just tried defining float operator+(float f1, float
    > f2), but the compiler's on to me :)


    You can't overload operators with only built-in types as parameters.
     
    Rolf Magnus, Jul 8, 2004
    #12
  13. Glen Able

    Glen Able Guest

    "Rolf Magnus" <> wrote in message
    news:ccjq8s$af7$02$-online.com...
    > Glen Able wrote:
    >
    > >> If you want an operator to concatenate strings with integers there is
    > >> nothing stopping you writing it.
    > >>
    > >> string operator+(const string& lhs, int rhs)
    > >> {
    > >> ...
    > >> }

    > >
    > > Ooh, didn't know you could do global operator overloads, ta.
    > > In my excitement I just tried defining float operator+(float f1, float
    > > f2), but the compiler's on to me :)

    >
    > You can't overload operators with only built-in types as parameters.


    Evidently :)

    Shame though. Would have been nice on a few occasions to help me trap dodgy
    floating point operations. I did once even try globally redefining 'float'
    as 'MyFloat' and supplying all the relevant operators, but ISTR there were
    some unresolvable problems which would've meant fixing the usage in numerous
    places. Actually, let me start a new thread on that...

    cheers,
    G.A.
     
    Glen Able, Jul 8, 2004
    #13
  14. Glen Able

    SaltPeter Guest

    "Glen Able" <> wrote in message
    news:ccj0ng$53r$1$...
    > Obviously the following doesn't work:
    >
    > int i = 5;
    > std::string myString = "Number is " + i + " thankyou please";
    >
    > So can anyone give me some idea what's the nicest way to this sort of

    thing?
    > I'm sure it's not using sprintf.
    >
    > with thanks,
    > G.A.
    >


    Why not consider a templated function? This way you can ostream other types,
    not just an int.

    // TtoStr.h
    #include <string>
    #include <sstream>

    template<class T> std::string TtoStr(const T& r_t)
    {
    std::eek:stringstream ossbuffer;
    ossbuffer << r_t;
    return ossbuffer.str();
    }

    // main
    #include <iostream>
    #include "TtoString.h"

    int main()
    {
    int i = 5;
    double d = 2.0;

    std::string s("int i = ");
    s += TtoStr(i);
    s += " and double d = ";
    s += TtoStr(d);

    std::cout << s << std::endl;

    return 0;
    }
     
    SaltPeter, Jul 9, 2004
    #14
    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.

Share This Page