Generally, the general STL algorithm and the string class method are enough to solve the problem. The reason for extending STD: string is that STL is cross-platform. In Windows, if different GUI Libraries are used, specific string classes are used, such as the cstring class of MFC/ATL, qstring class in QT and wxWidgets in wxWidgets. The latter two are fine and can be used directly. However, cstring is not cross-platform, and if the legacy code is string, it is usually changed significantly when it is to be transplanted to the corresponding GUI. The previous practice was to write an idatatypeconvertor class and encapsulate common data type conversion algorithms into static methods of the class, as shown in (conversion of cstring and STD: string ):
In this way, every time a static method is called, the number of times is too large, so it is necessary to extend the string so that it can be used in the usual way (such as the format/replace method of cstring ).
The extension method is inheritance, and two articles are well written. For details, refer:
String extension in STL to integrate it with cstring of VC
Http://blog.csdn.net/b2b160/archive/2009/07/27/4384020.aspx
Compile a cstring class in STL
Www.vckbase.com/code/relateddoc.asp? Id = 2049
Refer to the extension class implemented in the first article. If the replace function is defined in the CPP file outside the class, an error will occur during the link, but there will be no problem when you put it into a header file, and there are still some questions.
# Ifndef stdstringex0000hh <br/> # define stdstringex0000hh </P> <p> # include <atlstr. h> // For lpctstr <br/> # include <string> <br/> template <class T> <br/> class stdstringeximpl: public T <br/>{< br/> Public: <br/> stdstringeximpl (void) {}< br/> // implicit type conversion <br/> stdstringeximpl (lpctstr ptext): T (ptext ){}; <br/> // implicit type conversion <br/> stdstringeximpl (const T & pold): T (pold) {}; <br /> ~ Stdstringeximpl (void) {}< br/> Public: <br/> // cstring: replace <br/> bool Replace (lpctstr poristring, lpctstr pnewstring) <br/>{< br/> int nstart = 0; <br/> nstart = find (poristring); <br/> If (nstart <0) <br/> return false; </P> <p> while (nstart> = 0) <br/> {<br/> Replace (nstart, _ tcslen (poristring ), pnewstring); </P> <p> nstart + = _ tcslen (pnewstring); <br/> nstart = find (poristring, nstart ); <br/>}< br/> return true; <br/>}< br/> }; <br/> # endif // stdstringex0000hh </P> <p> // for Unicode <br/> # ifdef _ Unicode <br/> typedef stdstringeximpl <STD :: wstring> stdstringex; <br/> # else <br/> typedef stdstringeximpl <STD: String> stdstringex; <br/> # endif
Usage:
# Include "stdstringex. H "<br/> # include <iostream> <br/> using namespace STD; <br/> void main () <br/>{< br/> stdstringex STR; <br/> STR = _ T ("abcdef"); // all available string functions can be directly used <br/> cout <STR <Endl; <br/> Str. replace (_ T ("A"), _ T ("B"); // call the replacement function in cstring mode <br/> cout <"replacing... "<Endl <STR <Endl; <br/> getchar (); <br/>}
If you do not want to use a template, write it directly:
"Class stdstringex: public STD: String <br/>{< br/> };
Yes. Other methods can be added like this, such as the format method, so that the string class can be extended according to your own requirements. In the second article, all the methods of cstring are implemented, and then general classes can be used in the project, which is quite convenient.