Advertisement

Does anyone use Microsoft's CString class?

Started by July 19, 2000 02:06 PM
12 comments, last by Trajar 24 years, 5 months ago
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
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 formatingstring 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.
Advertisement
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..
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;} 
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.
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
Advertisement
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?
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.
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.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

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.
For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.

This topic is closed to new replies.

Advertisement