Jump to content
  • Advertisement

Archived

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

Trajar

Does anyone use Microsoft's CString class?

This topic is 6517 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

I was thinking of using the CString class in my game (it has lots of formatting features) but I worry about how optimized and whether or not it is fast enough to be a utility class. Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
CString does have some useful features, but I can''t think of any that you don''t get with std::string. The only exception is Format(), but you can get the same results using std::stringstream. If you want to keep the printf style syntax, you could write your own format function for std::string. Something like:


//printf style formating
string StrFormat(char *pszString, ...)
{
va_list argPtr;
//buffer to hold the string - this is a fixed size, but you probably wont run over it
char szBuf[32768];

//format the string
va_start (argPtr, pszString);
vsprintf (szBuf, pszString, argPtr);
va_end (argPtr);

//set the string to the result of the format
return string(szBuf);
}


If your using inside a loop you could make szBuf static try and make it a bit faster. If speed isn''t that much of a problem you might as well use stringstream.

Share this post


Link to post
Share on other sites
Do you use the SGI version of the STL? Do you have any code that I may look at, as I am not very good with the STL yet...

I appreciate it..

Share this post


Link to post
Share on other sites
I use the STL that comes with MSVC, I haven''t had any problems with it yet. the standard string class is pretty easy to use. If your using MSVC, just typing "std::string::" should give you a list of all the member functions (dunno how much help it''ll be for you though). The MSDN help isn''t much good for the STL stuff, If you got the cash a book on the standard C++ library would probably be a good idea (try "The Standard C++ Library" by Nicolai M. Josuttis, it worked for me). If not, your best of looking for some examples on line. I had a quick look but didn''t find much. Here''s a quick example of std::string:


#include
#include

using namespace std;


int main(int argc, char* argv[])
{
string str("Hello"); //create a string containing "Hello"

str+= " World"; //append " World"

string str2(str); //make a copy of str

if(str == str2) //compair (case sensetive) with ==
cout << "same" << endl;

int pos = str.find(''e''); //find the first ''e'' in str

cout << "e found at " << pos << endl;

int pos2 = str2.find_first_not_of("Hle"); //find the first char that is not ''H'' or ''l'' or ''e''

cout << "found" << str2[pos2] << " at " << pos2 << endl; //use [] to access single chars

const char* psz = str.c_str(); //.c_str() returns a char*

cout << psz << endl;

str = "";
if(str.empty()) //true if str is empty
cout << "str is empty" << endl;

cout << "str2 is " << str2.length() << " letters long" << endl;

return 0;
}

Share this post


Link to post
Share on other sites
Here''s an STL tutorial you might want to have a look at : http://www.infosys.tuwien.ac.at/Research/Component/tutorial/prwmain.htm no string stuff though (it''s not really part of the STL). It''s pretty old, so it''s using the old header files. You''ll want to use them without the .h and all the STL stuff lives in the std namespace.

Share this post


Link to post
Share on other sites
I downloaded the new SGI STL lib and tried putting in the namespace declaration and get the following error:

E:\Projects\VC 6\Font\v2\WinMain.cpp(19) : error C2871: ''std'' : does not exist or is not a namespace

I tried including and , nothing works

Thanks

Share this post


Link to post
Share on other sites
I dont use SGI''s STL, so i can''t help you there.

quote:

E:\Projects\VC 6\Font\v2\WinMain.cpp(19) : error C2871: ''std'' : does not exist or is not a namespace



That looks like a VC error to me, have you tried using the STL that comes with it?

Share this post


Link to post
Share on other sites
I got everything to work OK w/the generic STL that ships with VC 6, but wanted to get some better functionality with SGI''s version. I posted a new post about my problem, if you want to check it out.

For now, anyways, I''m using and just fine. Thanks for the help.

Share this post


Link to post
Share on other sites
One thing that CString does that I don''t think the STL''s string does is lazy splitting. If, for example, you create a duplicate string via the copy constructor or = operator, it just bumps the reference count and both strings point to the same buffer. If you make a change to one of the strings, the string then creates its own buffer, drops the refcount in the original, and makes the change in the string.

It''s a clever way to save some space but, IMHO, it''s a pretty minor advantage and will probably save you some space only if you were making a program with a ton of text-handling.

Share this post


Link to post
Share on other sites
johnhattan: it makes a big speed difference. The new and delete operators are slow.

Anyway I usually either use my own class that I made, or C strings.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!