C++ String Class Type

Started by
11 comments, last by Pho 22 years, 11 months ago
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
Advertisement
Why don´t you have a look at the help?

What the hells!
What the hells!
Why don´t you have a look at the help?

What the hells!
What the hells!
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!
What the hells!
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!
What the hells!
#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
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 :-)
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.
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.
Thank you.

That is all I need to know.

Pho

This topic is closed to new replies.

Advertisement