Home > Archive > VC STL > February 2005 > Re: string formatting..
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: string formatting..
|
|
| Jacklondon Chen 2005-02-14, 9:11 pm |
| You can write a new class named as TypeConvert or something like that. And
add a function as this:
string TypeConvert::Int2String(int value)
{
ostrstream strm;
strm << value ;
return strm.str();
}
In later time, you can use like this:
string s = TypeConvert::Int2String(n);
just one line code.
"BekTek" <bektek@gmail.com> 写入消息新闻:e37%23tBc0EHA.1296@TK2MSFTNGP10.phx.gbl...
> Usually,, when I work with std::string..
> I sometimes feel uncomfortable with that..
>
> CString has IMO greate function 'Format' ..
> but std::string doesn't..
>
> That's why I need some more extra code like this..
>
> char buff[1000];
> vsprintf("%d", n);
> std::string s = buff;;
>
> or
>
> ostrstream strm;
> strm << n ;
> std::string s = strm.str();
>
> something like these..
>
> But it also need 3 or more lines of code..
> Because I'm so lazy,, I want to make em working in 1 lines..
>
> any idea?
>
> do i need to stick to use MFC's CString?
>
>
>
| |
| Ulrich Eckhardt 2005-02-14, 9:11 pm |
| Jacklondon Chen wrote:
> You can write a new class named as TypeConvert or something like that. And
> add a function as this:
>
> string TypeConvert::Int2String(int value)
> {
> ostrstream strm;
> strm << value ;
> return strm.str();
> }
>
> In later time, you can use like this:
>
> string s = TypeConvert::Int2String(n);
>
> just one line code.
Why write a class just to get a function? Did you perhaps mean a namespace?
Anyhow:
#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>( 42);
int n = boost::lexical_cast<int>("42");
Alternatively, and for more control, use boost's format library.
Uli
| |
| Jason Winnebeck 2005-02-14, 9:11 pm |
| Yeah, no need for a class. If you were going to do that technique though,
at least make it a template function, so that it works for any type, not
just int.
Jason
Jacklondon Chen wrote:
> You can write a new class named as TypeConvert or something like that. And
> add a function as this:
>
> string TypeConvert::Int2String(int value)
|
|
|
|
|