CStringArray is a container class for MS VC , we write a sort function for it. Function declaration: void sort (CSTRINGARRAY & CA, // Sort Object Bool Ascending, // True = Ascending, false = Descendance; Bool CaseSensitive; // True = Different cases, false = ignore the case to implement sort with C and C methods, first look at C method. ================= C achieved === ================================== We use the QSort function of the C standard library. Qsort's statement is like this: Void Qsort (Void * Base, SIZE_T NUM, SIZE_T WIDTH, INT (Const Void * Elem1, Const Void * ELEM2); these parameters: base = ca.getdata (); Num = ca.getsize () Width = SIZEOF (CSTRING); The fourth parameter is a pointer to the "Compare" function. This "comparison" function must be provided by us, but the problem is that the "Compare" function can only accept two parameters, we can't put Ascending and CaseSensitive Passing to it, that is, we need 4 "comparison" functions. Below is the declaration of these four "comparison" functions: int compare 00 (const void * elem1, const void * elem2); int COMPARE01 (Const void * Elem1, Const void * elem2; int compare 10 (const void * elem1, const void * elem2); int compare11 (const void * elem1, const void * elem2); the number behind Compare corresponds to the value of Ascending and CaseSensitive, such as COMPARE10 represents ascending = True, CaseSensitive = false. Let's see the implementation of Sort: void sort (CStringArray & Ca, Bool Ascending, Bool CaseSensitive) {INT f = 0; if (ascending) f = 2; if (CaseSensitive) f | = 1; Switch (f) {case 0x00: Qsort (ca.getdata (), CA . Max (), compare00); Break; Case 0x01: Qsort (ca.getdata (), ca.getsize (), sizeof (cstring), compare01); Break;
Case 0x10: Qsort (ca.getdata (), ca.getsize (), sizeof (cstract), compare10; break; case 0x11: qsort (ca.getdata (), ca.getsize (), sizeof (cstring), Compare11 ); 4 "comparison" function is also easy to implement, for example: int compare00 (const void * elem1, const void * elem2) {return ((CString *) ELEM1) -> Comparenocase (* (* ((CString) *) ELEM2) * - 1);} ====================================================================================================== ================== We use C STL SORT Function Templates: Templatevoid Sort (Ranit First, Ranit Last, PRED PR);
Where parameters:
FigSt = ca.getdata ();
Last = ca.getdata () ca.getsize ();
The third parameter is a "Function Object", we don't imagine 4 "comparison" functions like it.
It is designed to design a "comparison" class:
Class CstringCompare
{
PUBLIC:
CStringCompare (Bool Ascending, Bool CaseSensitive)
{
CaseSensitive
M_PCompFunc = CSTRING :: Compare;
Else
m_pcompfunc = cstring :: Comparenocase;
m_asc = ascending;
}
Bool Operator () (Const CSTRING & CS1, Const Cstring & CS2) Const
{
RETURN (M_ASC? (CS1. * m_pcompfunc) (CS2) <0: ((CS1. * m_pcompfunc) (CS2)> = 0));
}
protected:
Typedef int (cstring :: * comparememberfunc) (const tchar *) const;
Comparememberfunc m_pcompfunc;
BOOL M_ASC;
}
SORT C implementation:
Void Sort (CStringArray & Ca, Bool Ascending, Bool CaseSensitive)
{
CStringCompare CP (Ascending, CaseSensitive);
Sort (ca.getdata (), ca.getdata () ca.getsize (), CP);
}
======================================================================================================================================================================================================================================== ========= Speed test, it will find that the implementation of C is slower than C,
This is because many temporary CSTRINGs are required during C .
Let's improve:
Class CstringCompare
{
PUBLIC:
Typedef struct
{
Char Data [Sizeof (CString)];
} Cstring;
CStringCompare (CStringArray & Ca, Bool Ascending, Bool CaseSensitive: M_CA (CA)
{
CaseSensitive
M_PCompFunc = CSTRING :: Compare;
Else
m_pcompfunc = cstring :: Comparenocase;
m_asc = ascending;
}
Bool Operator () (Const CSTRING & CS1, Const Cstring & CS2) Const
{
const cstring * pcs1 = reinterpret_cast
(& CS1);
Const CString * PCS2 = Reinterpret_cast
(& CS2);
RETURN (M_ASC? (PCS1 -> * m_pcompfunc) (* PCS2) <0
: ((PCS1 -> * m_pcompfunc) (* PCS2)> = 0))
}
CString * Begin ()
{
Return (ReinterPret_cast
(m_ca.getdata ()));
}
CString * end ()
{
Return (ReinterPret_cast
(m_ca.getdata () m_ca.getsize ()));
}
protected:
Typedef int (cstring :: * comparememberfunc) (const tchar *) const;
Comparememberfunc m_pcompfunc;
BOOL M_ASC;
CSTRINGARRAY & M_CA;
}
Void Sort (CStringArray & Ca, Bool Ascending, Bool CaseSensitive)
{
CSTRINGCompare CP (CA, Ascending, CaseSensitive);
sort (cp.begin (), cp.end (), cp);
}
Now the speed is almost, but is it safe to use ReinterPret_cast? Analyze look. Compile in numerous C
In the device, the MS VC is a good implementation, but it should be seen until the latest VC7, put the standard C (ISO98) in one
Many features are not realized in a dark position. Some people say that they will use VC to understand C , it is probably coming.