converting integer to a string(without the help of stringstreamvariable)

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

    converting integer to a string(without the help of stringstreamvariable)

    Hi,
    I am using a string variable in which I do lot of appending. The
    only difficulty I am facing as of now is appending a integer/float
    value to this variable. Although I can accomplish this task using a
    stringstream variable, it makes my life difficult as it involves 2
    function call(one for adding the information to stringstream variable
    and the other for copying that value in to string variable) with the
    additional burden of resetting this stringstream variable.

    Is there anyway I can accomplish this task in a much simpler
    way. Thanx in advance.

  • Martin York

    #2
    Re: converting integer to a string(without the help of stringstreamvar iable)

    On Feb 27, 10:22 pm, mthread <rjk...@gmail.c omwrote:
    Hi,
    I am using a string variable in which I do lot of appending. The
    only difficulty I am facing as of now is appending a integer/float
    value to this variable. Although I can accomplish this task using a
    stringstream variable, it makes my life difficult as it involves 2
    function call(one for adding the information to stringstream variable
    and the other for copying that value in to string variable) with the
    additional burden of resetting this stringstream variable.
    >
    Is there anyway I can accomplish this task in a much simpler
    way. Thanx in advance.
    Check out the library provided by boost.org

    There is a lexical_cast<>( ) function.

    It uses stringstream underneath but provides a nice interface to it.

    Comment

    • Juha Nieminen

      #3
      Re: converting integer to a string(without the help of stringstreamvar iable)

      mthread wrote:
      Hi,
      I am using a string variable in which I do lot of appending. The
      only difficulty I am facing as of now is appending a integer/float
      value to this variable. Although I can accomplish this task using a
      stringstream variable, it makes my life difficult as it involves 2
      function call(one for adding the information to stringstream variable
      and the other for copying that value in to string variable) with the
      additional burden of resetting this stringstream variable.
      >
      Is there anyway I can accomplish this task in a much simpler
      way. Thanx in advance.
      AFAIK the only alternative *standard* way to do that would be to use
      std::sprintf(), but that would probably not be any simpler than using
      stringstreams. On the contrary, it would probably be more complicated
      because you would have to guess how many characters std::sprintf() will
      write, and make sure you allocate enough space for it. Then you have to
      resize the string to fit the actual amount of characters written.

      Comment

      • Michael DOUBEZ

        #4
        Re: converting integer to a string(without the help of stringstreamvar iable)

        mthread a écrit :
        Hi,
        I am using a string variable in which I do lot of appending. The
        only difficulty I am facing as of now is appending a integer/float
        value to this variable. Although I can accomplish this task using a
        stringstream variable, it makes my life difficult as it involves 2
        function call(one for adding the information to stringstream variable
        and the other for copying that value in to string variable) with the
        additional burden of resetting this stringstream variable.
        >
        Is there anyway I can accomplish this task in a much simpler
        way. Thanx in advance.
        >
        If you do a lot of appending, using a stringstream from the very
        beginning seems logical. And with it, you could control the formating of
        your numerical values (which is not the case with boost::lexical_ cast<>).


        Michael

        Comment

        • Jim Langston

          #5
          Re: converting integer to a string(without the help of stringstream variable)

          mthread wrote:
          Hi,
          I am using a string variable in which I do lot of appending. The
          only difficulty I am facing as of now is appending a integer/float
          value to this variable. Although I can accomplish this task using a
          stringstream variable, it makes my life difficult as it involves 2
          function call(one for adding the information to stringstream variable
          and the other for copying that value in to string variable) with the
          additional burden of resetting this stringstream variable.
          >
          Is there anyway I can accomplish this task in a much simpler
          way. Thanx in advance.
          I also ran into this problem a bit and I finally made a template.

          template<typena me T, typename F T StrmConvert( const F from )
          {
          std::stringstre am temp;
          temp << from;
          T to = T();
          temp >to;
          return to;
          }

          template<typena me Fstd::string StrmConvert( const F from )
          {
          return StrmConvert<std ::string>( from );
          }

          Basically this allows me to do things like:

          std::string MyString;
          MyString += "Some Value: " + StrmConvert( someint ) + " some float:" +
          StrmConvert( somefloat );

          etc.. All in one line.

          Note there is no error checking, so say, for example, you wanted to pull a
          number from a string:

          std::string Data = "abc";
          int MyInt = StrmConvert<int >( Data );

          Data would contain 0 since the variable in StrmConert is default
          constructed. It will not throw or even indicate there was an error other
          than the default constructed value. This works for me. If it doesn't work
          for you, check the status of the stringstream temp in StrmConvert and if it
          is not okay throw something.


          --
          Jim Langston
          tazmaster@rocke tmail.com


          Comment

          Working...