Does anyone use Microsoft's CString class?
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:
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.
//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.
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 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
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
I dont use SGI''s STL, so i can''t help you there.
That looks like a VC error to me, have you tried using the STL that comes with it?
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.
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.
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.
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.
Anyway I usually either use my own class that I made, or C strings.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement