std::string and integers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glen Able

    std::string and integers

    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.


  • Tim Love

    #2
    Re: std::string and integers

    "Glen Able" <smeREMOVEckler s@hotMEmail.com > writes:
    [color=blue]
    >Obviously the following doesn't work:[/color]
    [color=blue]
    >int i = 5;
    >std::string myString = "Number is " + i + " thankyou please";[/color]
    [color=blue]
    >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.[/color]

    You can use cout-style formatting to print to a string. See the FAQ



    Comment

    • Gwar

      #3
      Re: std::string and integers



      On Thu, 8 Jul 2004, Glen Able wrote:
      [color=blue]
      > 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.
      >
      >
      >[/color]
      // 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;

      }

      Comment

      • Anders Persson

        #4
        Re: std::string and integers

        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:
        [color=blue]
        > 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.[/color]

        Comment

        • Rolf Magnus

          #5
          Re: std::string and integers

          Anders Persson wrote:
          [color=blue]
          > try
          > int i = 5;
          > std::string myString;
          > myString = "Number is " + i + " thankyou please";
          > // ANders[/color]

          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.

          Comment

          • Glen Able

            #6
            Re: std::string and integers


            "Gwar" <xeno@xor.qua > wrote in message
            news:2004070801 4542.U17241@syn ergy.transbay.n et...[color=blue]
            >[color=green][color=darkred]
            >> > Obviously the following doesn't work:[/color]
            > >
            > > int i = 5;
            > > std::string myString = "Number is " + i + " thankyou please";[/color]
            >[/color]
            [color=blue]
            > 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;[/color]

            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";




            Comment

            • Evan Carew

              #7
              Re: std::string and integers

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

              Glen Able wrote:[color=blue]
              > "Gwar" <xeno@xor.qua > wrote in message
              > news:2004070801 4542.U17241@syn ergy.transbay.n et...
              >[color=green][color=darkred]
              >>>>Obviously the following doesn't work:
              >>>
              >>>int i = 5;
              >>>std::strin g myString = "Number is " + i + " thankyou please";[/color]
              >>[/color]
              >[color=green]
              >> 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;[/color]
              >
              >
              > 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";
              >[/color]
              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<sh ort>(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+wAJ 9R3yYSkpBSmORTE oGarWyCvTxAhQCe Nm0L
              trw57xPoS3zrpil JjmpQc28=
              =e9Og
              -----END PGP SIGNATURE-----

              Comment

              • John Harrison

                #8
                Re: std::string and integers

                > There is, its called lexical cast & is part of the Boost library. look[color=blue]
                > at http://boost.org/libs/conversion/lexical_cast.htm
                >
                > The code looks like the following:
                >
                > string mystring;
                > mystring = "The number is " + lexical_cast<sh ort>(5) + " thank you,[/color]
                please";[color=blue]
                >[/color]

                Errm

                lexical_cast<st ring>(5)

                john


                Comment

                • John Harrison

                  #9
                  Re: std::string and integers


                  "Glen Able" <smeREMOVEckler s@hotMEmail.com > wrote in message
                  news:ccjbi2$dua $1$8300dec7@new s.demon.co.uk.. .[color=blue]
                  >
                  > "Gwar" <xeno@xor.qua > wrote in message
                  > news:2004070801 4542.U17241@syn ergy.transbay.n et...[color=green]
                  > >[color=darkred]
                  > >> > Obviously the following doesn't work:
                  > > >
                  > > > int i = 5;
                  > > > std::string myString = "Number is " + i + " thankyou please";[/color]
                  > >[/color]
                  >[color=green]
                  > > 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;[/color]
                  >
                  > 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";
                  >[/color]

                  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


                  Comment

                  • Glen Able

                    #10
                    Re: std::string and integers

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

                    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 :)
                    [color=blue]
                    > Don't blame me if the results don't match your expectations though.
                    >
                    > john[/color]

                    Do you have some specific problem in mind, John?

                    ta,
                    G.A.


                    Comment

                    • Rolf Magnus

                      #11
                      Re: std::string and integers

                      Glen Able wrote:
                      [color=blue][color=green]
                      >> If you want an operator to concatenate strings with integers there is
                      >> nothing stopping you writing it.
                      >>
                      >> string operator+(const string& lhs, int rhs)
                      >> {
                      >> ...
                      >> }[/color]
                      >
                      > 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 :)[/color]

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

                      Comment

                      • Glen Able

                        #12
                        Re: std::string and integers

                        "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                        news:ccjq8s$af7 $02$1@news.t-online.com...[color=blue]
                        > Glen Able wrote:
                        >[color=green][color=darkred]
                        > >> If you want an operator to concatenate strings with integers there is
                        > >> nothing stopping you writing it.
                        > >>
                        > >> string operator+(const string& lhs, int rhs)
                        > >> {
                        > >> ...
                        > >> }[/color]
                        > >
                        > > 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 :)[/color]
                        >
                        > You can't overload operators with only built-in types as parameters.[/color]

                        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.


                        Comment

                        • SaltPeter

                          #13
                          Re: std::string and integers


                          "Glen Able" <smeREMOVEckler s@hotMEmail.com > wrote in message
                          news:ccj0ng$53r $1$830fa7a5@new s.demon.co.uk.. .[color=blue]
                          > 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[/color]
                          thing?[color=blue]
                          > I'm sure it's not using sprintf.
                          >
                          > with thanks,
                          > G.A.
                          >[/color]

                          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::ostringstr eam 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;
                          }


                          Comment

                          • Rolf Magnus

                            #14
                            Re: std::string and integers

                            Glen Able wrote:
                            [color=blue]
                            > Eek! Why isn't there anything nice, like a std::string ctor that
                            > takes an int,[/color]

                            There is. It will create a string of the specified size.


                            Comment

                            Working...