如何利用C++ vector的sort算法对元素是自定义类型进行排序

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>    //sort函数头文件
 
using namespace std;
 
class Student {
public:
    int m_num;
    string m_name;
 
public:
 
    Student(int n,string name)
        :m_num(n),m_name(name)
    {
 
    }
 
    void display() const{
        cout << "姓名:" << m_name << " ,学号:" << m_num << endl;
    }
};
//自定义的比较函数,这里是对学号进行排序
static bool myCompare(const Student& a1,const Student& a2)
{
    return a1.m_num <= a2.m_num;
}
 
int main()
{
    //创建4个学生
    vector<Student> classOne;
    Student a1(2,"a");
    classOne.push_back(a1);
    Student a2(4,"b");
    classOne.push_back(a2);
    Student a3(5,"c");
    classOne.push_back(a3);
    Student a4(1,"d");
    classOne.push_back(a4);
 
    cout << "排序前:" << endl;
    vector<Student>::iterator it = classOne.begin();
    while (it != classOne.end())
    {
        it->display();
        ++it;
    }
 
    sort(classOne.begin(), classOne.end(),myCompare);
    cout << "排序后:" << endl;
    it = classOne.begin();
    while (it != classOne.end())
    {
        it->display();
        ++it;
    }
    return 0;
}
<pre name="code" class="cpp">需求:

客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序。

数据存储:

每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面。

老的解决方法:

冒泡排序方法算法

新的方法:

参考http://blog.csdn.net/aastoneaa/article/details/8471722,总结使用C++自带的std::sort函数。

详细方法:

军团对象,定义了军团的各种简单属性


class AllianceData
{
public:
    AllianceData();
 
    ~AllianceData();
 
    std::string uid;//唯一key
 
    std::string aname;//名称
 
    nv_uint32 num;//成员总数
 
    nv_uint32 alv;//等级
};
 定义用来比较的函数对象,我们命名为CompHelper.h


#ifndef _CompHelper_H_
#define _CompHelper_H_
 
#include "AllianceData.h"<br>
//升序排列
class CompLess 
{ 
public: 
    bool operator ()(const AllianceData& pstItem1, const AllianceData pstItem2) 
    { 
        return pstItem1.alv < pstItem2.alv; 
    }  <br>};
//降序排列
class CompGreater 
{ 
public: 
    bool operator ()(const AllianceData& pstItem1, const AllianceData& pstItem2) 
    { 
        return pstItem1.alv > pstItem2.alv; 
    } 
  
}; 
 
 
#endif // _CompHelper_H_
 我们调用时这样:
#ifndef _CompHeloer_H_
#define _CompHeloer_H_
 
#include "AllianceData.h"
#include "ArmyData.h"
#include "EquipmentData.h"
 
//升序排列
class CompLess 
{ 
public: 
    bool operator ()(const AllianceData& pstItem1, const AllianceData pstItem2) 
    { 
        return pstItem1.alv < pstItem2.alv; 
    } 
    bool operator ()(const CommanderData& pstItem1, const CommanderData pstItem2) 
    { 
        return pstItem1.lv < pstItem2.lv; 
    } 
    bool operator ()(const EquipmentData& pstItem1, const EquipmentData pstItem2) 
    { 
        return pstItem1.eid < pstItem2.eid; 
    } 
};
//降序排列
class CompGreater 
{ 
public: 
    bool operator ()(const AllianceData& pstItem1, const AllianceData& pstItem2) 
    { 
        return pstItem1.alv > pstItem2.alv; 
    } 
    bool operator ()(const CommanderData& pstItem1, const CommanderData pstItem2) 
    { 
        return pstItem1.lv > pstItem2.lv; 
    } 
    bool operator ()(const EquipmentData& pstItem1, const EquipmentData pstItem2) 
    { 
        return pstItem1.eid > pstItem2.eid; 
    } 
}; 
 
 
#endif // _CompHeloer_H_
 总结:

排序的方法有很多中,可参考http://blog.csdn.net/aastoneaa/article/details/8471722。

通过参考这个,我个人觉得函数对象的方法使用最方便,维护量少,而且统一。





<pre name="code" class="cpp">#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
class AbA
{
public:
int m_nA;
int m_nB;
AbA(int a, int b) : m_nA(a), m_nB(b){}
};
 
ostream& operator << (ostream& os, const AbA& ra)
{
os << ra.m_nA << " " << ra.m_nB;
return os;
}
 
// 自己写的比较函数
bool newLess(const AbA& ra, const AbA& rb)
{
return (ra.m_nA < rb.m_nA );
}
int main()
{
vector<AbA> vecAbA;
vecAbA.push_back(AbA(1, 2));
vecAbA.push_back(AbA(7, 8));
vecAbA.push_back(AbA(5, 6));
vecAbA.push_back(AbA(3, 4));
 
sort(vecAbA.begin(), vecAbA.end(), newLess);
for (int i = 0; i < vecAbA.size(); i++)
{
cout << vecAbA[i] << " ";
}
return 0;
}

    原文作者:排序算法
    原文地址: https://blog.csdn.net/superldxu/article/details/53106324
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞