Archived

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

Trajar

Does anyone use Microsoft's CString class?

Recommended Posts

Trajar    122
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
Wilka    122
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
Trajar    122
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
Wilka    122
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
Wilka    122
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
Trajar    122
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
Wilka    122
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
Trajar    122
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
johnhattan    2346
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
Wilka    122
quote:

blue-lightning



Providing you pass your strings by const&, the different probably wont be that big in a game. string will allocate more memory that it needs, so if you append to the string it doesn''t need to allocate more memory.

Share this post


Link to post
Share on other sites
johnhattan    2346
Blue, don''t see why they should be significantly slower than a simpler string. The only added overhead is checking and incrementing a use-count. In fact, copy constructors and assignment operators should be significantly faster, as they only increment a use-count.

Share this post


Link to post
Share on other sites
Goblin    122
I find it amusing that Microsoft made their own specific string class... now they''re going onto their own specific programming language.

They should make a whole new set of variable types!!!:

microsoftINTtm count;
microsoftCHARtm key;
microsoftFLOATtm speed;

Soon Starbucks and Microsoft will merge, and we''ll all be getting Microsoft(tm) brand coffee... eww..

Maybe I''m just going crazy or something.

- Goblin
"A woodchuck would chuck as much wood as a woodchuck could if a woodchuck could chuck wood. Deal."

Share this post


Link to post
Share on other sites
johnhattan    2346
In MS''s defense, CString existed years before there was any kind of standard string class. CString shipped with MFC 1.0, circa 1991. Ditto for the string and collection in Borland''s BIDS library, circa 1990.

Around that time, the only standard class of any type was "complex", and that''s only because it was detailed in Stroustrup''s book.

Share this post


Link to post
Share on other sites