Archived

This topic is now archived and is closed to further replies.

C++ String Class Type

This topic is 6077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all! Up until now, I used to tend the standard C-style character strings in all my OpenGL projects. You know, something like: const char *string = “How are you today?”; int iWidth = 640; char text[255]; sprintf( text, “Window width: %d”, iWidth ); Just recently I learned about this really cool standard C++ string class type. Basically, you include the following header: #include "string" and then use it like this: std::string sWindowName( “NeHe OpenGL Framework”); My problem concerns the way to write formatted data to a string. For instance, when using the old C style strings, I would do something like this: int iWidth = 640; int iHeight = 480; char text[255]; sprintf( text, “Width is: %d, Height is: %d”, iWidth, iHeight ); MessageBox( hWnd, text, “Window Dimensions”, MB_OK ); So does anybody know how to use this kind of formatting ( mixing strings with numbers particularly ) with the C++ string class type ? I would be thankful for any possible tips. Pho Edited by - Pho on April 24, 2001 8:30:27 PM Edited by - Pho on April 24, 2001 8:31:13 PM

Share this post

Share on other sites
Why don´t you have a look at the help?

What the hells!

Share this post

Share on other sites
Why don´t you have a look at the help?

What the hells!

Share this post

Share on other sites
Why don´t you have a look at the help?

I think you can concatenate strings using +:

using namespace std;    string Str,StrYears;    Str+="I´m "+ StrYears+"old";     cout<[\code]Then look for a method to return a char* in order to use ShowMessage.(In Borland´s VCL it is called c_str().)

What the hells!

Share this post

Share on other sites
Uhmmmmm SORRY about the mulltiple posts.......
I pressed tab key to indent....
Then I pressed enter key twice...

It is too late here in Spain!(2:43).

Good night...

What the hells!

Share this post

Share on other sites
#include "sstream"

....
int iWidth = 640;
int iHeight = 480;
std::ostringstream text;
text << "Width is: " << iWidth << ", Height is: " << iHeight << std::ends;
MessageBox( hWnd, text.str().c_str(), “Window Dimensions”, MB_OK );
....

Good luck, Ben.

Edited by - bharris on April 24, 2001 12:37:06 AM

Share this post

Share on other sites
correct me if I''m false but in my implemention it seems that ostrstream::str() returns a char* to a buffer (not automatically terminated by \0). So the line
MessageBox( hWnd, text.str().c_str(), "Window Dimensions", MB_OK );
should be
MessageBox( hWnd, text.str(), Window Dimensions, MB_OK );
call "text.freeze(0);" after this to set back the side effects text.str() had before (look in your help for more).
I recommend you to learn more about streams, because this is the most common way formated output is done in C++.
A formated output directly to a string instance isn''t available as far as I know.
There is also no equivalent function for printf/ sprintf for strings or streams. But you can write your own one, it shouldn''t be that hard.

Greetings Ben (also :-)

Share this post

Share on other sites
Ben, my code isn''t using the older std::ostrstream class (which indeed returns a character pointer), but rather std::ostringstream (take a closer look at what I posted), which returns an std::string (a copy, that is) of the string that the ostringstream object represents, when the overloaded str() member function is called (with no arguments).

To clear the ostringstream, invoke the other overloaded str() member function with a null string, ie
text.str("");

Cheers, Ben.

Share this post

Share on other sites
Quote from _Ben
"A formated output directly to a string instance isn''t available as far as I know. There is also no equivalent function for printf/sprintf for strings or streams. But you can write your own one, it shouldn''t be that hard."

Not true. std::ostringstream and std::istringstream operate in a very similar way to sprintf and sscanf directly on std::string''s. It is not advisable to rewrite functionality provided by your implementation when its part of the language definition.

Ben.

Share this post

Share on other sites
Thank you.

That is all I need to know.

Pho

Share this post

Share on other sites
I would be glad if there is a fact i missed. I''ve never seen sth. about ostringstream and istringstream.
I only know about ostrstream and. Is the i/ostringstream class part of standard C++ or isn''t it?
Wich Compiler do you use?

Curios Ben

P.S.Will test it on MS VC++ as soon as I''m home again

Share this post

Share on other sites
Ben_, std::ostringstream and std::istringstream (std::stringstream), are indeed defined as part of the C++ standard. Providing your compiler complies with the language definition, as of 1998, it should support the "sstream" header file.

Regards, Ben.

Share this post

Share on other sites
Thanks, I''ve found it.
I will have to rewrite some of my code now. This class is really helpful and I was angry that there wasn''t one like that out there!!

Greetings Ben

• Forum Statistics

• Total Topics
628745
• Total Posts
2984488

• 12
• 25
• 12
• 10
• 17