自定义结构体vector排序
2011-07-12 15:27
267 查看
// 列表行内容
typedef struct _LIST_ROW
{
int iSeq; // 序号
DWORD dwId; // 表中的Id号
TCHAR szEmp[64]; // 用户名
TCHAR szIP[20]; // IP
TCHAR szMAC[20]; // MAC
TCHAR szType[32]; // 类型
TCHAR szValid[32]; // 有效期
TCHAR szBeg[32]; // 开始时间
TCHAR szEnd[32]; // 结束时间
TCHAR szRemark[32]; // 备注
int iMode; // 工作模式
}LIST_ROW, *PLIST_ROW;
/* 以下为vector排序函数 */
bool lessfun_seq(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iSeq < lr2.iSeq;
}
bool greaterfun_seq(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iSeq > lr2.iSeq;
}
bool lessfun_emp(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEmp, lr2.szEmp);
return (ret < 0) ? true : false;
}
bool greaterfun_emp(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEmp, lr2.szEmp);
return (ret > 0) ? true : false;
}
bool lessfun_ip(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int nIP1 = (int)htonl(inet_addr(lr1.szIP));
int nIP2 = (int)htonl(inet_addr(lr2.szIP));
return nIP1 < nIP2;
}
bool greaterfun_ip(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int nIP1 = (int)htonl(inet_addr(lr1.szIP));
int nIP2 = (int)htonl(inet_addr(lr2.szIP));
return nIP1 > nIP2;
}
bool lessfun_mac(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szMAC, lr2.szMAC);
return (ret < 0) ? true : false;
}
bool greaterfun_mac(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szMAC, lr2.szMAC);
return (ret > 0) ? true : false;
}
bool lessfun_type(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szType, lr2.szType);
return (ret < 0) ? true : false;
}
bool greaterfun_type(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szType, lr2.szType);
return (ret > 0) ? true : false;
}
bool lessfun_beg(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szBeg, lr2.szBeg);
return (ret < 0) ? true : false;
}
bool greaterfun_beg(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szBeg, lr2.szBeg);
return (ret > 0) ? true : false;
}
bool lessfun_end(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEnd, lr2.szEnd);
return (ret < 0) ? true : false;
}
bool greaterfun_end(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEnd, lr2.szEnd);
return (ret > 0) ? true : false;
}
bool lessfun_mode(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iMode < lr2.iMode;
}
bool greaterfun_mode(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iMode > lr2.iMode;
}
/**
* 函数名称:SortList
* 函数功能:列表排序
* 函数参数:nCol 按第几列
* nType 升序 0 降序 1
*/
void CByPassDlg::SortList(int nCol, int nType)
{
switch (nCol)
{
case 0:// 按序号排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_seq);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_seq);
break;
case 1:// 按用户名排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_emp);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_emp);
break;
case 2:// 按IP排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_ip);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_ip);
break;
case 3:// 按MAC排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_mac);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_mac);
break;
case 4:// 按授信类型排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_type);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_type);
break;
case 5:// 按开始时间排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_beg);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_beg);
break;
case 6:// 按结束时间排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_end);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_end);
break;
case 7:// 按工作模式排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_mode);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_mode);
break;
}
}
void CByPassDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
int nSubItem = pNMListView->iSubItem;
// 若原来为升序,则为降序,若原来为降序,则为升序
m_aSortType[nSubItem] = (m_aSortType[nSubItem] == 0) ? 1 : 0;
SortList(nSubItem, m_aSortType[nSubItem]);
m_listCtrl.RedrawItems(0, m_vecListRow.size());
*pResult = 0;
}
注:有时需要用stable_sort.
stable_sort与sort的区别是:保证相等的元素原本相对次序在排序后保持不变。
例如,如下的排序规则就必须使用stable_sort,因为sort会造成排序永远完成不了。
// 顺序: 禁用-下线-启用
bool lessfun_effective(const INNERNET_RECORD &lr1, const INNERNET_RECORD &lr2)
{
if (lr1.nEffective == 0 /*&& (lr1.nEffective == 0 || lr2.nEffective == 1)*/)
{// 两者均是禁用或者前者是禁用、后者是启用,则不必调换
return true;
}
else if (lr1.nEffective == 1 && lr2.nEffective == 0)
{// 前者是启用,后者是禁用,则调换
return false;
}
else
{// 两者都是启用,则根据在线状态决定是否调换
return lr1.nOnline < lr2.nOnline;
}
}
// 顺序:启用-下线-禁用
bool greaterfun_effective(const INNERNET_RECORD &lr1, const INNERNET_RECORD &lr2)
{
if (/*(lr1.nEffective == 1 || lr1.nEffective == 0) && */lr2.nEffective == 0)
{// 前者是启用、后者是禁用或者两者都是禁用,则不必调换
return true;
}
else if (lr1.nEffective == 0 && lr2.nEffective == 1)
{// 前者是禁用,后者是启用,则调换
return false;
}
else
{// 两者都是启用,则根据在线状态决定是否调换
return lr1.nOnline > lr2.nOnline;
}
}
stable_sort(rvecList.begin(), rvecList.end(), lessfun_effective);
stable_sort(rvecList.begin(), rvecList.end(), greaterfun_effective);
即,排序同时参照结构体的两个元素的值。
也可以将排序函数封装到类里面,但是必须定义为static类型的。
typedef struct _LIST_ROW
{
int iSeq; // 序号
DWORD dwId; // 表中的Id号
TCHAR szEmp[64]; // 用户名
TCHAR szIP[20]; // IP
TCHAR szMAC[20]; // MAC
TCHAR szType[32]; // 类型
TCHAR szValid[32]; // 有效期
TCHAR szBeg[32]; // 开始时间
TCHAR szEnd[32]; // 结束时间
TCHAR szRemark[32]; // 备注
int iMode; // 工作模式
}LIST_ROW, *PLIST_ROW;
/* 以下为vector排序函数 */
bool lessfun_seq(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iSeq < lr2.iSeq;
}
bool greaterfun_seq(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iSeq > lr2.iSeq;
}
bool lessfun_emp(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEmp, lr2.szEmp);
return (ret < 0) ? true : false;
}
bool greaterfun_emp(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEmp, lr2.szEmp);
return (ret > 0) ? true : false;
}
bool lessfun_ip(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int nIP1 = (int)htonl(inet_addr(lr1.szIP));
int nIP2 = (int)htonl(inet_addr(lr2.szIP));
return nIP1 < nIP2;
}
bool greaterfun_ip(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int nIP1 = (int)htonl(inet_addr(lr1.szIP));
int nIP2 = (int)htonl(inet_addr(lr2.szIP));
return nIP1 > nIP2;
}
bool lessfun_mac(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szMAC, lr2.szMAC);
return (ret < 0) ? true : false;
}
bool greaterfun_mac(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szMAC, lr2.szMAC);
return (ret > 0) ? true : false;
}
bool lessfun_type(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szType, lr2.szType);
return (ret < 0) ? true : false;
}
bool greaterfun_type(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szType, lr2.szType);
return (ret > 0) ? true : false;
}
bool lessfun_beg(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szBeg, lr2.szBeg);
return (ret < 0) ? true : false;
}
bool greaterfun_beg(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szBeg, lr2.szBeg);
return (ret > 0) ? true : false;
}
bool lessfun_end(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEnd, lr2.szEnd);
return (ret < 0) ? true : false;
}
bool greaterfun_end(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
int ret = _tcsicmp(lr1.szEnd, lr2.szEnd);
return (ret > 0) ? true : false;
}
bool lessfun_mode(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iMode < lr2.iMode;
}
bool greaterfun_mode(const LIST_ROW &lr1, const LIST_ROW &lr2)
{
return lr1.iMode > lr2.iMode;
}
/**
* 函数名称:SortList
* 函数功能:列表排序
* 函数参数:nCol 按第几列
* nType 升序 0 降序 1
*/
void CByPassDlg::SortList(int nCol, int nType)
{
switch (nCol)
{
case 0:// 按序号排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_seq);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_seq);
break;
case 1:// 按用户名排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_emp);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_emp);
break;
case 2:// 按IP排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_ip);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_ip);
break;
case 3:// 按MAC排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_mac);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_mac);
break;
case 4:// 按授信类型排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_type);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_type);
break;
case 5:// 按开始时间排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_beg);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_beg);
break;
case 6:// 按结束时间排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_end);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_end);
break;
case 7:// 按工作模式排序
if (nType == 0)
sort(m_vecListRow.begin(), m_vecListRow.end(), lessfun_mode);
else if (nType == 1)
sort(m_vecListRow.begin(), m_vecListRow.end(), greaterfun_mode);
break;
}
}
void CByPassDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
int nSubItem = pNMListView->iSubItem;
// 若原来为升序,则为降序,若原来为降序,则为升序
m_aSortType[nSubItem] = (m_aSortType[nSubItem] == 0) ? 1 : 0;
SortList(nSubItem, m_aSortType[nSubItem]);
m_listCtrl.RedrawItems(0, m_vecListRow.size());
*pResult = 0;
}
注:有时需要用stable_sort.
stable_sort与sort的区别是:保证相等的元素原本相对次序在排序后保持不变。
例如,如下的排序规则就必须使用stable_sort,因为sort会造成排序永远完成不了。
// 顺序: 禁用-下线-启用
bool lessfun_effective(const INNERNET_RECORD &lr1, const INNERNET_RECORD &lr2)
{
if (lr1.nEffective == 0 /*&& (lr1.nEffective == 0 || lr2.nEffective == 1)*/)
{// 两者均是禁用或者前者是禁用、后者是启用,则不必调换
return true;
}
else if (lr1.nEffective == 1 && lr2.nEffective == 0)
{// 前者是启用,后者是禁用,则调换
return false;
}
else
{// 两者都是启用,则根据在线状态决定是否调换
return lr1.nOnline < lr2.nOnline;
}
}
// 顺序:启用-下线-禁用
bool greaterfun_effective(const INNERNET_RECORD &lr1, const INNERNET_RECORD &lr2)
{
if (/*(lr1.nEffective == 1 || lr1.nEffective == 0) && */lr2.nEffective == 0)
{// 前者是启用、后者是禁用或者两者都是禁用,则不必调换
return true;
}
else if (lr1.nEffective == 0 && lr2.nEffective == 1)
{// 前者是禁用,后者是启用,则调换
return false;
}
else
{// 两者都是启用,则根据在线状态决定是否调换
return lr1.nOnline > lr2.nOnline;
}
}
stable_sort(rvecList.begin(), rvecList.end(), lessfun_effective);
stable_sort(rvecList.begin(), rvecList.end(), greaterfun_effective);
即,排序同时参照结构体的两个元素的值。
也可以将排序函数封装到类里面,但是必须定义为static类型的。
相关文章推荐
- 简谈俩种不同方式对vector容器的 自定义结构体进行排序
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- 自定义结构体vector的排序
- C++中,自定义结构体vector的排序
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- vector中自定义结构体的排序
- (sort对结构体自定义排序+vector)L2-009. 抢红包
- 使用STL去除std::vector自定义结构体重复项
- find_if和函数对象结合实现对自定义类型vector的查找 复杂结构体查询某个值
- ZOJ 3770 : Ranking System (第14届浙江大学程序设计竞赛 D) - 结构体排序,vector
- vector按存储的结构体的某一字段排序
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- 使用直接插入排序原理对自定义的Vector进行排序
- vector自定义数据结构数组按照指定元素排序
- 【面试题】sizeof 获取自定义类,结构体,vector,map,string的占内存大小
- C++中的结构体vector排序
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- vector中struct等自定义结构排序
- c++中sort的vector中结构体排序的两种方法说明