📄 std_string.shtml
字号:
<HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Zafir Anjum"> <TITLE>String - CString like class using STL</TITLE></HEAD><body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed"><table WIDTH="100%"><tr WIDTH="100%"><td align=center><!--#exec cgi="/cgi/ads.cgi"--><td></tr></table><CENTER><H3><FONT COLOR="#AOAO99">CString like class using STL</FONT></H3></CENTER><CENTER><H3><HR></H3></CENTER><p>This article was contributed by <A HREF="mailto:jmoleary@earthlink.net">Joe O'Leary</A>. <P>I don't know how "cool" this class is -- it's not really innovationas much as hard work -- but I have found it extremely useful. I haveseen attempts at this in the past that I never really liked verymuch... <P>Basically, I love the ease of use of MFC's CString class, but Idon't want to use MFC. In fact, I don't want to rely on any classlibrary's string class because I want portability. The Standard C++Library string classes (string and wstring) are OK, but they arenowhere near as easy to use as CString. I decided to combine the bestof both worlds and create:<PRE><TT><FONT COLOR="#990000"> CStdString</FONT></TT></PRE> <P>This is a string class derived from the Standard C++ Librarybasic_string<TCHAR> and so inherits all of basic_string'sfunctionality and compatibility). You can use it in along with MFC,ATL or neither. I have added a few (not all) of the CStringconveniences that I've found most convenient. Here is a summary ofthe functionality <ol> <li>Like CString, CStdString is built on TCHAR and so easily changesfrom UNICODE to MBCS builds <li>Since it is derived from basic_string<TCHAR> you can use itit wherever you would use basic_string<TCHAR> <li>It provides an implicit conversion operator LPCTSTR() so youdon't have to call c_str() all the time. ANSI doesn't like this but Ido. <li>It corrects a bug in the MS implementation of basic_string --the need to call erase() before assigning one string to another (seeknowledge base article Q172398) <li>It includes the following CString like functions: <PRE><TT><FONT COLOR="#990000"> Format() TrimRight(), TrimLeft() </FONT></TT></PRE><br> a Load() function that loads a string value from string resource ID A constructor that lets you pass a string resource ID wrapped by MAKEINTRESOURCE() - (this actually works now)<li>If you use CStdString in an MFC build (i.e. when _MFC_VER isdefined) it adds global serialization operators so you can read andwrite CStdStrings to CArchives<li>When used in non-ATL, non-MFC builds, the header file also definesUNICODE/MBCS conversion operators identical to those of MFC and ATL(i.e. A2CT, T2W, etc)<li>It does not dynamically allocate any memory in the constructor andtherefore avoids the potential danger of the fact that basic_string'sdestructor is non-virtual<li>Most functions are inline for speed<li>Case sensitive or insensitive comparison via the Equals() memberfunction. (operator== and < are case sensitive always)<li>a static SetResourceHandle() member function if you want it tosearch another HINST first when attempting to Load() strings (a thinimitation of AfxSetResourceHandle())<li>When used in non-ATL, non-MFC builds, the header file also definesUNICODE/MBCS conversion operators identical to those of MFC and ATL(i.e. A2CT, T2W, etc)</ol> <P>I have found this class invaluable for a large-scale distributedproject on which I worked in which some components were MFC, some wereATL, and some were neither. The class is portable won't conflict witheither class library.<P>There are a couple of things to keep in mind when using this class. <ol> <li>The reference counting logic of MS' implemenation ofbasic_string is unchanged. It uses operator++ and -- which are NOTthread safe. I meant to address this but never got around to it. Ithas never caused me any problems (and I've used it in severalmulti-threaded apps) but there you have it. <li>Even though this class fixes an MS bugs, you can probably use itwith any implementation of basic_string (i.e. SGI's, et al). I don'tthink I have used any knowledge of the basic_string implementation.The fix is just a single, in-line call to erase() so I don't think itwill slow anyone down.</ol><p>This version is a fix to the first one I posted on this website. Thefixes are:<p>Added some overloads to the assign() member function to further catchcases of the MS assignment bug that I had missed.<p>Fixed some typos that checked for "MFC_VER" when they should have beenchecking for "_MFC_VER" (note missing underscore)<p>Now there REALLY is a constructor that takes a string resource ID. Inthe first version I never managed to put it in. So now you can dostuff like this:<pre><tt><font COLOR="#990000"> CStdString myString(MAKEINTRESOURCE(IDS_SOME_STRING_ID));</font></tt></pre>The improvements are:<ul> <li>Added a constructor and operator=() for the new _bstr_t compilersupport class that comes with Visual C++ 5.0. This eliminatescompiler ambiguity errors that you would otherwise receive if youtried to assign a _bstr_t to a CStdString. It does not preclude us ofthe class in VC version 4.x, however. <li>Cleaner, shorter, and faster implementations of Load() andFormat() member functions. <li>Added improved ASSERT macro that for non-MFC, non-ATL builds(uses _ASSERTE instead of DebugBreak) <li>Added case-insensitive comparison (Equals() member function). <li>ToLower(), ToUpper(), TrimLeft(), and TrimRight() now return*this so you can chain them together if you want. For example::<pre><tt><font COLOR="#990000"> CStdString myString("JOE "); myString.ToLower().TrimLeft().TrimRight().</font></tt></pre></ul><P>Hope someone else finds this as useful as I have. <P><A HREF="StdString.zip">Download Source</A> 9KB<!-- Remember to update this --><p>Last updated: 9 May 1998<P><HR><TABLE BORDER=0 WIDTH="100%" ><TR><TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD><TD WIDTH="33%"> <CENTER><FONT SIZE=-2>© 1998 Zafir Anjum</FONT> </CENTER></TD><TD WIDTH="34%"><DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV></TD></TR></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -