string formating 
Author Message
 string formating

Hi All,

I am changing some code over from using MFC CString to std::string.  The
one thing that I'm immediately missing is the CString::Format(...)
method.  I don't see a comparable method in the std::string object.  I
guess I could use sprintf(...) if I had to, but was wondering if the STL
offers a way to format a string similar to sprintf() within the STL.

Any ideas?

Thanks in advance,

--Ed



Mon, 04 Apr 2005 04:17:53 GMT  
 string formating

Quote:
> I am changing some code over from using MFC CString to std::string.  The
> one thing that I'm immediately missing is the CString::Format(...)
> method.  I don't see a comparable method in the std::string object.  I
> guess I could use sprintf(...) if I had to, but was wondering if the STL
> offers a way to format a string similar to sprintf() within the STL.

No.  And yes.

sprintf is one way to do it, of course, and in some cases it's the way to
go.

However, for outputting complex data (i.e. objects), you want to look at
string streams.

Basically:

std::string strTest = "For all good men ";
std::ostringstream sstr;
CMyObject myobj;  // supports overloaded << operations.

sstr << "Now is the time " << strTest << 1234 << myobj;

// now sstr holds a concatenation of all of that information.  To use it we
can:

std::string strOutput = sstr.str();

// or

const char *pszOutput = sstr.str().c_str();



Mon, 04 Apr 2005 04:42:27 GMT  
 string formating
When I need to do something like this, I use ostringstream. This way,
you can do anything that you can do with cout.  Then call
ostringstream::str() to get a string object.

--
Steven Velez
Software Engineer
alventive



Mon, 04 Apr 2005 04:48:49 GMT  
 string formating

Quote:

>No.  And yes.

>sprintf is one way to do it, of course, and in some cases it's the way to
>go.

>However, for outputting complex data (i.e. objects), you want to look at
>string streams.

>Basically:

>std::string strTest = "For all good men ";
>std::ostringstream sstr;
>CMyObject myobj;  // supports overloaded << operations.

>sstr << "Now is the time " << strTest << 1234 << myobj;

>// now sstr holds a concatenation of all of that information.  To use it we
>can:

>std::string strOutput = sstr.str();

Fine.

Quote:
>// or

>const char *pszOutput = sstr.str().c_str();

Not so fine. The problem is that str() returns a std::string by value,
and this temporary is destroyed at the end of the full-expression that
produces it. Thus, pszOutput is left a dangling pointer.

--
Doug Harrison
Microsoft MVP - Visual C++



Mon, 04 Apr 2005 12:20:13 GMT  
 string formating

 > Hi All,
 >
 > I am changing some code over from using MFC CString to std::string.
 > The one thing that I'm immediately missing is the
 > CString::Format(...) method.  I don't see a comparable method in
 > the std::string object.  I guess I could use sprintf(...) if I had
 > to, but was wondering if the STL offers a way to format a string
 > similar to sprintf() within the STL.
 >

You might want to have a look at the boost.format library at
www.boost.org. It gives you the flexibility of printf combined with
the typesafety of C++.

HTH, Markus



Mon, 04 Apr 2005 16:38:43 GMT  
 string formating


Quote:
> >std::string strOutput = sstr.str();

> Fine.

> >// or

> >const char *pszOutput = sstr.str().c_str();

> Not so fine. The problem is that str() returns a std::string by value,
> and this temporary is destroyed at the end of the full-expression that
> produces it. Thus, pszOutput is left a dangling pointer.

Sorry.  Slipped my mind.

Corrected:

std::string strOutput = sstr.str();
const char *pszOutput = strOutput.c_str();

Thanks fo the catch.



Mon, 04 Apr 2005 22:11:13 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Formating a string

2. Formating String output

3. RichTextBox Text Formating

4. Automatic DateTime formating between TimeZones?

5. Date formating

6. Formating Input :: simulating 2 prompts on 1 screen

7. screen formating

8. Currency formating

9. printf formating in different compilers

10. Looking for Currency formating function!

11. Formating Number and use malloc without free

12. Formating output !

 

 
Powered by phpBB® Forum Software