-
December 8th, 2003, 02:57 AM
#1
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
-
December 8th, 2003, 03:12 AM
#2
std::string doesn't support such functionalities, use std::stringstream instead.
-
December 8th, 2003, 03:27 AM
#3
can you explain me about std::stringstream?
how do I do sprintf with it?
Does it have a function that return std::string?
thanks
-
December 8th, 2003, 03:29 AM
#4
std::string has a member function called c_str() which returns a char*.
sprintf(szStr.c_str(), "%.2f", dNum);
see if that works.
-
December 8th, 2003, 03:36 AM
#5
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 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();
};
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."
-
December 8th, 2003, 04:14 AM
#6
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."
-
December 8th, 2003, 04:42 AM
#7
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.
-
December 8th, 2003, 04:44 AM
#8
... 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
-
December 8th, 2003, 04:57 AM
#9
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."
-
December 8th, 2003, 06:24 AM
#10
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
-
December 9th, 2003, 08:39 AM
#11
what is stringstream
I found it diffcult to find info about it?
what is it purpose
thanks
-
December 9th, 2003, 07:30 PM
#12
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.
-
January 7th, 2004, 04:53 AM
#13
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 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();
};
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|