Hello,
I waas wondering how do I do this using std::string instead of char*
sprintf(szStr, "%.2f", dNum);
thanks
Printable View
Hello,
I waas wondering how do I do this using std::string instead of char*
sprintf(szStr, "%.2f", dNum);
thanks
std::string doesn't support such functionalities, use std::stringstream instead.
can you explain me about std::stringstream?
how do I do sprintf with it?
Does it have a function that return std::string?
thanks
std::string has a member function called c_str() which returns a char*.
sprintf(szStr.c_str(), "%.2f", dNum);
see if that works.
Hi, take a look at the following templates. I believe this
is covered in the FAQ as well
So your example is implemented asPHP 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 t, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::ostringstream oss;
oss <<std::fixed<<std::setprecision(precision)<< f << t;
return oss.str();
};
//from std::string to a number
template <typename T>
bool convert_from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::istringstream iss(s);
return !(iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
};
YourString = convert_to_string(dNum, std::dec, 2);
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.
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.
... and, of course, std::string::c_str() returns a const char *, so you can't pass it to sprintf, anyway.
sprintf(const_cast<char*>(szStr.c_str()), "%.2f", dNum);
Oh the horror
:eek:
souldog: please don't use language like that, there may children reading this. :p
what is stringstream
I found it diffcult to find info about it?
what is it purpose
thanks
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.
Quote:
Originally posted by souldog
Hi, take a look at the following templates. I believe this
is covered in the FAQ as well
So your example is implemented asPHP 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 t, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::ostringstream oss;
oss <<std::fixed<<std::setprecision(precision)<< f << t;
return oss.str();
};
//from std::string to a number
template <typename T>
bool convert_from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&), const std::streamsize precision)
{
std::istringstream iss(s);
return !(iss>>f>>std::setprecision(precision)>>std::fixed>>t).fail();
};
YourString = convert_to_string(dNum, std::dec, 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