[MSDN] How to use QSort to sort the inheritance class of Carray

xiaoxiao2021-03-06  50

HOWTO: Quick Sorting Using MFC Carray-Derived Classes ID: Q216858

The Information in this Article Applies TO:

Microsoft Visual C , 32-Bit Editions, Versions 5.0, 6.0

Summary

This Article Shows You How To Quick-Sort AN MFC Carray-Derived Class. The Code Below Depends Only On The MFC and Visual C Run-Time Library.

More information

The Visual C Run-Time Library (MSVCRT) Implements The Quick-Sort Function, Qsort, As Follows:

Void Qsort (void * base, size_t num, size_t width, int (__cdecl * company) (const void * elem2);

The following example shows you how to use this function to sort an MFC CArray class. Although the following example uses a CString array, you can easily customize for other MFC CArray-derived classes, such as CByteArray, CDWordArray, CObArray, CPtrArray, CUintArray, And cwordArray.

Steps to Implement Sorting

Derive Your Array Data Class from One of the Carray-Derived Classes. IN Our EXAMPLE, WE Use CstringArray As The Base Class for Our Class. The Following Is The Declaration In The Heooter File:

Class CsortableStringArray: Public CSTRINGARRY: PUBLIC

{

PUBLIC:

protected:

}; To the public section of your class add the folload function:

Void Sort (StringComparefn PfnCompare = Compare); To the protected section of your class address:

static int __cdecl Compare (const CString * pstr1, const CString * pstr2);. Now add the following two type defs above the declaration of your class These typedefs later help us in passing pointers of the two functions that we declared in steps 2 and 3 To The Visual C Run-Time's Qsort Function:

Typedef int (__cdecl * genericcomparefn) (Const Void * ELEM2);

TYPEDEF INT (Const CString * Elem1, Const Cstring * Elem2); in Your .cpp file, IMPLEMENT THE TWO FUNCTIONS That You Declared Earlier: //

// sortableStringArray.cpp

//

INT CsortablestringArray :: Compare (const cstring * pstr2)

{

Assert (PSTR1);

AskERT (PSTR2);

Return PSTR1-> Compare (* PSTR2);

}

Void CsortableStringArray :: Sort (Stringcomparefn Pfncompare / * = CsortedStringArray :: Compare * /)

{

CString * prgstr = getdata ();

Qsort (prgstr, getsize (), sizeof (cstring), (genericcomparefn) pfncompare;

}

That Is It. Now Let's Look At the Contents of The Entire File:

//

// sortableStringArray.h

//

Typedef int (__cdecl * genericcomparefn) (Const Void * ELEM2);

TYPEDEF INT (CONST CSTRING * ELEM1, Const Cstring * ELEM2);

Class CsortableStringArray: Public CSTRINGARRY: PUBLIC

{

PUBLIC:

Void Sort (StringComparefn PFNCompare = Compare);

protected:

Static int __cdecl company (const cstring * pstr1, const cstring * pstr2);

}

//

// sortableStringArray.cpp

//

#include "sortableStringArray.h"

INT CsortablestringArray :: Compare (const cstring * pstr2)

{

Assert (PSTR1);

AskERT (PSTR2);

Return PSTR1-> Compare (* PSTR2);

}

Void CsortableStringArray :: Sort (Stringcomparefn Pfncompare / * = CsortedStringArray :: Compare * /)

{

CString * prgstr = getdata ();

Qsort (prgstr, getsize (), sizeof (cstring), (genericcomparefn) pfncompare;

}

To use the array in your code, Just Declare it and call the Sort () function. The folload example Uses this Array:

SRAND TIME (NULL); // generate seed for rand ().

CsortableStringArray Arr;

CString Str;

For (int i = 0; i <1000; i )

{

Str.Format ("% 6d", Rand ()); // Get a random number string.

Arr.Add (STR);

Trace ("% S / N", (LPCTSTSTSTST) STR);

}

Long ltim = gettickcount ();

Arr.sort ();

For (i = 0; i <1000; i )

{

Trace ("% S / N", (LPCTSTST) Arr [I]);

}

TRACE ("Time TOOK =% li / n", GetTickCount () - LTIM);

NOTE: To implement a CArray type other than CString, just derive your class from the other array types and modify the Sort () and the Compare () functions accordingly.NOTE: Quick Sort is a more efficient mechanism of sorting than the bubble sort used In The Microsoft Knowledge Base Article, "How to Sort A CstringArray In MFC" (Q120961)

References

For Additional Information About The Bubble Sort Method, Please See The Following Article In The Microsoft Knowledge Base:

Q120961 HOW SORT A CSTRINGARRAY IN MFC

Additional Query Words:

KeyWords: KBDatabase KBMFC KBODBC KBVC500 KBVC600 Version: Winnt: 5.0, 6.0 Platform: Winnt Issue Type: Kbhowto

Last Reviewed: February 4, 2000 © 2000 Microsoft Corporation. All Rights Reserved. Terms of Use.

转载请注明原文地址:https://www.9cbs.com/read-82565.html

New Post(0)