# CStringArray排序

CStringArray是MS VC++的一个容器类, 我们为它写一个排序函数.

void Sort(CStringArray &ca,    //排序对象
BOOL ascending,	   //TRUE=升序, FALSE=降序;
BOOL caseSensitive);  //TRUE=区别大小写, FALSE=忽略大小写

================= C 的实现 =====================================

void qsort(void *base, size_t num, size_t width, int(__cdecl *compare)(const void *elem1, const void *elem2 ));

base = ca.GetData();
num  = ca.GetSize();
width= sizeof(CString);

int compare00(const void *elem1, const void *elem2);
int compare01(const void *elem1, const void *elem2);
int compare10(const void *elem1, const void *elem2);
int compare11(const void *elem1, const void *elem2);

compare后面的数字对应于ascending和caseSensitive的值,例如compare10表

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.GetSize(), sizeof(CString), compare00);
break;
case 0x01:
qsort(ca.GetData(), ca.GetSize(), sizeof(CString), compare01);
break;
case 0x10:
qsort(ca.GetData(), ca.GetSize(), sizeof(CString), compare10);
break;
case 0x11:
qsort(ca.GetData(), ca.GetSize(), sizeof(CString), compare11);
break;
}

}

4个"比较"函数也很容易实现, 例如:
int compare00(const void *elem1, const void *elem2)
{
return(((CString*)elem1)->CompareNoCase(*((CString*)elem2))*-1);
}

================= C++ 的实现 =====================================

template

void sort(RanIt first, RanIt last, Pred pr);

first=ca.GetData();
last =ca.GetData()+ca.GetSize();

class CStringCompare
{
public:
CStringCompare(BOOL ascending, BOOL caseSensitive)
{
if(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++实现:

void Sort(CStringArray &ca, BOOL ascending, BOOL caseSensitive)
{
CStringCompare cp(ascending, caseSensitive);
sort(ca.GetData(), ca.GetData()+ca.GetSize(), cp);
}

================= C++ 的实现的改进 =====================================

class CStringCompare
{
public:
typedef struct {char data[sizeof(CString)];} CSTRING;
CStringCompare(CStringArray & ca, BOOL ascending, BOOL caseSensitive):m_CA(ca)
{
if(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);
}


• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
06-29
12-12 94
07-19 697
06-10 1503
03-24 396
06-09
04-27
02-22 141
02-21 281
02-23 234

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。