[VC(Visual C++) ]-MFC的CStringArrayy 資料排序
本篇要分享- MFC(VC6)的CStringArrayy 資料進行排序,有興趣的(C/P)同好,歡迎來(C/P)一下哈哈 ^ ^。
SortableStringArray.h
// SortableStringArray.h: interface for the CSortableStringArray class.//////////////////////////////////////////////////////////////////////// #if !defined(AFX_SORTABLESTRINGARRAY_H__349A568A_2C25_4BC9_A63F_CF6DCC5FD4CC__INCLUDED_) #define AFX_SORTABLESTRINGARRAY_H__349A568A_2C25_4BC9_A63F_CF6DCC5FD4CC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif// _MSC_VER > 1000 typedefint (__cdecl *GENERICCOMPAREFN)(constvoid * elem1, constvoid * elem2); typedefint (__cdecl *STRINGCOMPAREFN)(const CString * elem1, const CString * elem2); class CSortableStringArray : public CStringArray {public: CSortableStringArray();virtual ~CSortableStringArray(); void Sort(STRINGCOMPAREFN pfnCompare = Compare); protected: staticint __cdecl Compare(const CString * pstr1, const CString * pstr2); }; #endif// !defined(AFX_SORTABLESTRINGARRAY_H__349A568A_2C25_4BC9_A63F_CF6DCC5FD4CC__INCLUDED_)
|
SortableStringArray.cpp
// SortableStringArray.cpp: implementation of the CSortableStringArray class.//////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CStringArray_Sort.h" #include "SortableStringArray.h" #include <afxtempl.h> #ifdef _DEBUG #undef THIS_FILE staticchar THIS_FILE[]=__FILE__; #definenew DEBUG_NEW #endif //////////////////////////////////////////////////////////////////////// Construction/Destruction////////////////////////////////////////////////////////////////////// CSortableStringArray::CSortableStringArray(){ } CSortableStringArray::~CSortableStringArray(){ }int CSortableStringArray::Compare(const CString * pstr1, const CString * pstr2) { ASSERT(pstr1); ASSERT(pstr2);return pstr1->Compare(*pstr2); } void CSortableStringArray::Sort(STRINGCOMPAREFN pfnCompare /*= CSortedStringArray::Compare */) { CString * prgstr = GetData();qsort(prgstr,GetSize(),sizeof(CString),(GENERICCOMPAREFN)pfnCompare); }
|
CStringArray_Sort.cpp
// CStringArray_Sort.cpp : Defines the entry point for the console application.// #include "stdafx.h" #include "CStringArray_Sort.h" /////////////////////////////////#include "SortableStringArray.h" /////////////////////////////////#ifdef _DEBUG #definenew DEBUG_NEW #undef THIS_FILE staticchar THIS_FILE[] = __FILE__; #endif /////////////////////////////////////////////////////////////////////////////// The one and only application object CWinApp theApp; usingnamespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {// TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; }else {// TODO: code your application's behavior here. CSortableStringArray SASA; SASA.Add(CString("test_840K")); SASA.Add(CString("test_64K")); SASA.Add(CString("test_640K")); SASA.Add(CString("test_1240K"));for (int i = 0; i <= SASA.GetUpperBound(); i++) cout << SASA[i].GetBuffer(0) << endl; SASA.Sort(); cout <<"/////////////////////////////////"<< endl; for (int j = 0; j <= SASA.GetUpperBound(); j++) cout << SASA[j].GetBuffer(0) << endl; } return nRetCode; }
|
CStringArray_Sort.h
#if !defined(AFX_CSTRINGARRAY_SORT_H__86576243_E046_4134_B859_5B29476B9AF1__INCLUDED_) #define AFX_CSTRINGARRAY_SORT_H__86576243_E046_4134_B859_5B29476B9AF1__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif// _MSC_VER > 1000 #include "resource.h" #endif// !defined(AFX_CSTRINGARRAY_SORT_H__86576243_E046_4134_B859_5B29476B9AF1__INCLUDED_)
|
完整範例下載:http://yfdisk.com/file/jashliao/10cb3cf2/
參考資料出處:http://support.microsoft.com/kb/216858/zh-tw
沒有留言:
張貼留言