⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 std_string.shtml.htm

📁 mfc资料集合5
💻 HTM
字号:
<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" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><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 innovation as much as hard work --  but I have found it extremely useful.  I have seen attempts at this in the past that I never really liked very much...
 
<P>Basically, I love the ease of use of MFC's CString class, but I don't want to use MFC.  In fact, I don't want to rely on any class library's string class because I want portability.  The Standard C++ Library string classes (string and wstring) are OK, but they are nowhere near as easy to use as CString.  I decided to combine the best of both worlds and create:   

<PRE><TT><FONT COLOR="#990000"> 
     CStdString
</FONT></TT></PRE>
 
<P>This is a string class derived from the Standard C++ Library basic_string&ltTCHAR>  and so inherits all of basic_string's functionality and compatibility).  You can use it in along with MFC, ATL or neither.   I have added a few (not all) of the CString conveniences that I've found most convenient.  
 
<ol> 
<li>Like CString, CStdString is built on TCHAR and so easily changes from UNICODE to MBCS builds
<li>Since it is derived from basic_string&lt> you can use it it wherever you would use basic_string&lt>
<li>It provides an implicit conversion operator LPCTSTR() so you don't have to call c_str() all the time.  ANSI doesn't like this but I do.
<li>It corrects a bug in the MS implementation of basic_string -- the need to call erase() before assigning one string to another (see knowledge base article Q172398)
<li>It includes the following CString like functions:
<PRE><TT><FONT COLOR="#990000">
Format()
TrimRight(), TrimLeft()
</FONT></TT></PRE>

     a constructor that takes a string resource ID
<li>If you use CStdString in an MFC build (i.e. when _MFC_VER is defined) it also add global serialization operators so you can read and write CStdStrings to CArchives
<li>When used in non-ATL, non-MFC builds, the header file also defines UNICODE/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 and therefore avoids the potential danger of the fact that basic_string's destructor is non-virtual
<li>Most functions are inline for speed
 </ol>
 
<P>I have found this class invaluable for a large-scale distributed project on which I worked in which some components were MFC, some were ATL, and some were neither.  The class is portable won't conflict with either class library.  It even has a static SetResourceHandle() if you want it to search another HINST first when attempting to load strings (a thin imitation of AfxSetResourceHandle()).
 
<P>There are a couple of things to keep in mind when using this class.
 
<ol>
<li>The reference counting logic of MS' implemenation of basic_string is unchanged.  It uses operator++ and -- which are NOT thread safe.  I meant to address this but never got around to it.  It has never caused me any problems (and I've used it in several multi-threaded apps) but there you have it.
<li>Even though this class fixes an MS bugs, you can probably use it with any implementation of basic_string (i.e. SGI's, et al).  I don't think 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 it will slow anyone down.
 </ol>

<P>Hope someone else finds this as useful as I have.
 
<P><A HREF="StdString.zip" tppabs="http://www.codeguru.com/string/StdString.zip">Download Source</A> 9KB

<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%"> <CENTER><FONT SIZE=-2>&copy; 1997 Zafir Anjum</FONT>&nbsp;</CENTER></TD>
<TD WIDTH="34%"><DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A>&nbsp;</FONT></DIV></TD>
</TR>
</TABLE>
<CENTER><FONT SIZE=-2>4894</FONT></CENTER>
</BODY>
</HTML>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -