CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    how do I do that using stl

    Hello,

    I waas wondering how do I do this using std::string instead of char*
    sprintf(szStr, "%.2f", dNum);

    thanks

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    std::string doesn't support such functionalities, use std::stringstream instead.

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    can you explain me about std::stringstream?
    how do I do sprintf with it?
    Does it have a function that return std::string?

    thanks

  4. #4
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776
    std::string has a member function called c_str() which returns a char*.
    sprintf(szStr.c_str(), "%.2f", dNum);

    see if that works.

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Hi, take a look at the following templates. I believe this
    is covered in the FAQ as well

    PHP Code:
    //here are a template functions for converting between numbers to std::string.
    //the second parameter should be std::hex, std::oct or std::dec
    template <typename T>
    std::string convert_to_string(T tstd::ios_base & (*f)(std::ios_base&), const  std::streamsize precision)
    {
        
    std::ostringstream oss;
        
    oss <<std::fixed<<std::setprecision(precision)<< << t;
        return 
    oss.str();
    };

    //from std::string to a number
    template <typename T>
    bool convert_from_string(&t, const std::string &sstd::ios_base & (*f)(std::ios_base&), const  std::streamsize precision)
    {
        
    std::istringstream iss(s);
        return !(
    iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
    }; 
    So your example is implemented as
    YourString = convert_to_string(dNum, std::dec, 2);
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Oh, by the way, what Joe has suggested here is a very bad
    idea. The return value of std::string::c_str() is owned by the
    std::string object and should not be modified, nor deleted etc..
    If you do then you will find that you have a very unhappy
    std::string on your hands.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Good point, souldog. That's because the memory of std::string is self-managed. If the memory of std::string is being directly manipulated, its state is most likely be corrupted.

    By the way, I have find this similar thread which Andreas explained its usage.

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    ... and, of course, std::string::c_str() returns a const char *, so you can't pass it to sprintf, anyway.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  9. #9
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    sprintf(const_cast<char*>(szStr.c_str()), "%.2f", dNum);

    Oh the horror
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    souldog: please don't use language like that, there may children reading this.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  11. #11
    Join Date
    Sep 2003
    Posts
    815
    what is stringstream

    I found it diffcult to find info about it?
    what is it purpose

    thanks

  12. #12
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    It is stream type that self-manages its memory. By using stringstream, you are equivalent to using sprintf-favour functions and manually managing your memory allocation and deallocation.

    You can find information about STL from MSDN.

  13. #13
    Join Date
    Sep 2003
    Posts
    815
    Originally posted by souldog
    Hi, take a look at the following templates. I believe this
    is covered in the FAQ as well

    PHP Code:
    //here are a template functions for converting between numbers to std::string.
    //the second parameter should be std::hex, std::oct or std::dec
    template <typename T>
    std::string convert_to_string(T tstd::ios_base & (*f)(std::ios_base&), const  std::streamsize precision)
    {
        
    std::ostringstream oss;
        
    oss <<std::fixed<<std::setprecision(precision)<< << t;
        return 
    oss.str();
    };

    //from std::string to a number
    template <typename T>
    bool convert_from_string(&t, const std::string &sstd::ios_base & (*f)(std::ios_base&), const  std::streamsize precision)
    {
        
    std::istringstream iss(s);
        return !(
    iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
    }; 
    So your example is implemented as
    YourString = convert_to_string(dNum, std:ec, 2);

    if my dNum = -0.0001
    YourString will be "-0.00"
    and I need it to be "0.00"

    any idea for a simple solution

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured