c++中vector自定义排序的问题

如果要自己定义STL容器的元素类最好满足STL容器对元素的要求
    必须要求:
     1、Copy构造函数
     2、赋值=操作符
     3、能够销毁对象的析构函数
    另外:
     1、可用的缺省构造函数,序列型容器必须,用于初始化元素
     2、==操作符定义,用于判断相等
     3、<操作符定义,关联型容器必须,用于缺省排序

 

你可在struct內加入 operator < ,就可以使struct有排序能力.
因為而你的pcd struct內沒有指針,所以不須要有copy constructor
和copy assignment, 編譯器會為你提供的, 你不須要自己做的.
當你要排序時只要寫 sort( obj.begin(), obj.end() )就可.

 

以上内容取自帖子:http://bbs.csdn.net/topics/40228627

另一篇参考地址:http://blog.csdn.net/tigernana/article/details/7293758

以下取自帖子:http://blog.csdn.net/guang11cheng/article/details/7556697

三种方式实现vector的自定义排序

方法1:重载运算符

 #include <vector>
 #include <algorithm>
 #include <functional>

using namespace std;
struct TItem
{
    int m_i32Type;
    int m_i32ID;

    bool operator <(const TItem& rhs) const // 升序排序时必须写的函数
    {
        return m_i32Type < rhs.m_i32Type;
    }
    bool operator >(const TItem& rhs) const // 降序排序时必须写的函数
    {
        return m_i32Type > rhs.m_i32Type;
    }
};
int main()
{
    vector<TItem> stItemVec;


    TItem stItem1;
    stItem1.m_i32Type = 1;
    stItem1.m_i32ID = 1;

    TItem stItem2;
    stItem2.m_i32Type = 2;
    stItem2.m_i32ID = 2;

    TItem stItem3;
    stItem3.m_i32Type = 3;
    stItem3.m_i32ID = 3;

    TItem stItem4;
    stItem4.m_i32Type = 2;
    stItem4.m_i32ID = 4;

    stItemVec.push_back(stItem1);
    stItemVec.push_back(stItem2);
    stItemVec.push_back(stItem3);
    stItemVec.push_back(stItem4);

    // 升序排序
    sort(stItemVec.begin(), stItemVec.end(), less<TItem>()); 
    // 或者sort(ctn.begin(), ctn.end());   默认情况为升序

    for (size_t i = 0; i < stItemVec.size(); i++)
        printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

    printf("--\n");

    // 降序排序
    sort(stItemVec.begin(), stItemVec.end(), greater<TItem>());

    for (size_t i = 0; i < stItemVec.size(); i++)
        printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

    return 0;
}

  方法2:全局的比较函数

 #include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

bool lessmark(const TItem& stItem1, const TItem& stItem2)
 {
     return stItem1.m_i32Type < stItem2.m_i32Type;
 }
 

bool greatermark(const TItem& stItem1, const TItem& stItem2)
 {
     return stItem1.m_i32Type > stItem2.m_i32Type;
 }
 

int main()
 {
     vector<TItem> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(stItem1);
     stItemVec.push_back(stItem2);
     stItemVec.push_back(stItem3);
     stItemVec.push_back(stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), lessmark); //升序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
 

    printf("--\n");
 

    sort(stItemVec.begin(), stItemVec.end(), greatermark); //降序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
 

    return 0;
 }
 

方法3:函数对象

#include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

class CompLess
 {
 public:
     bool operator ()(const TItem& stItem1, const TItem& stItem2)
     {
         return stItem1.m_i32Type < stItem2.m_i32Type;
     }
 };
 

class CompGreater
 {
 public:
     bool operator ()(const TItem& stItem1, const TItem& stItem2)
     {
         return stItem1.m_i32Type > stItem2.m_i32Type;
     }
 };
 

int main()
 {
     vector<TItem> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(stItem1);
     stItemVec.push_back(stItem2);
     stItemVec.push_back(stItem3);
     stItemVec.push_back(stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), CompLess()); //升序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
 

    printf("--\n");
 

    sort(stItemVec.begin(), stItemVec.end(), CompGreater()); //降序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
 

    return 0;
 }
 

/*
 结果如下:
 type: 1, id: 1
 type: 2, id: 2
 type: 2, id: 4
 type: 3, id: 3
 --
 type: 3, id: 3
 type: 2, id: 2
 type: 2, id: 4
 type: 1, id: 1
 可以看出vector的sort的稳定的。
 */

问题:

1,示例代码中只有>和<关系处理,==关系是如何推导出来的?

2,排序时要移动元素,效率怎样?

3,如果自定义结构定义在一个类的内部,使用函数对象进行排序,这个函数对象可以作为类的成员函数吗?

4,在上面的例子中,vector中存放的都是结构(对象)本身,如果存放的是结构指针,该如何排序呢?此时只能通过全局的比较函数或者函数对象来做,且比较函数的参数要是指针类型的,如下:

(1)全局的比较函数

#include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

bool CompLess(const TItem* pstItem1, const TItem* pstItem2)
 {
     return pstItem1->m_i32Type < pstItem2->m_i32Type;
 }
 

bool CompGreater(const TItem* pstItem1, const TItem* pstItem2)
 {
     return pstItem1->m_i32Type > pstItem2->m_i32Type;
 }
 

int main()
 {
     vector<TItem*> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(&stItem1);
     stItemVec.push_back(&stItem2);
     stItemVec.push_back(&stItem3);
     stItemVec.push_back(&stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), CompLess); //升序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
 

    printf("--\n");
 

    sort(stItemVec.begin(), stItemVec.end(), CompGreater); //降序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
 
    return 0;
 }

(2)函数对象

#include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

class CompLess
 {
 public:
     bool operator ()(const TItem* pstItem1, const TItem* pstItem2)
     {
         return pstItem1->m_i32Type < pstItem2->m_i32Type;
     }
 };
 

class CompGreater
 {
 public:
     bool operator ()(const TItem* pstItem1, const TItem* pstItem2)
     {
         return pstItem1->m_i32Type > pstItem2->m_i32Type;
     }
 };
 

int main()
 {
     vector<TItem*> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(&stItem1);
     stItemVec.push_back(&stItem2);
     stItemVec.push_back(&stItem3);
     stItemVec.push_back(&stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), CompLess()); //升序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
 

    printf("--\n");
 

    sort(stItemVec.begin(), stItemVec.end(), CompGreater()); //降序排序
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
 

    return 0;
 }

推荐使用函数对象。

  • 13
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
本版本与网上其他资源不同之处在于,此版本可进行编辑,搜索,已进行内容识别扫描。可全选,可编辑,可剪切文字。 部分目录如下: 目录 第一篇预备知识 第1 章C++ 编程技术...................................................... 3 1-1 C++与C 语言的区别................................................... 4 1-1-1 文件扩展名的改变,.............................................. 4 1-1-2 简化输入/输出手段.............................................. 5 1-1-3 数据类型声明的改变,............................................ 5 1-1-4 动态内存分配运算符的使用....................................... 6 1-1-5 弓I 用(References) 类型, ··················•················•"'''8 1-1-6 const 语义的扩展................................................ 9 1-1-7 指针声明类型与对象类型相一致.................................. 13 1-1-8 int 与char 不再等价............................................. 13 1-1-9 结构数据类型的变化............................................ 13 1-1-10 数组和指针技术的不同......................................... 14 1-2 C++存储技术........................................................ 15 1-2 一I C++存储类型.................................................. 15 I6I7 ..... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. •. .• •. .• .. .. .. .. .. .. .. .. .. .. .. .. . 期 符存 饰生 修的 取象 存对 廿廿 I2I32 ~3 c c 1-3 C++ 函数技术........................................................ 19 1-3-1 类的构造函数、析构函数与赋值函数,..... - ........ - .............. 19 1-3-2 在派生类实现类的基本函数,................... _ ............... 29 1-3-3 内联函数技术,........ ................................... 30 3133 ..... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. . 现 实 术的 技制 数机 函象 元对 友向 由 曰1. l -C 1 4 3337 ..... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. . 术术 技技 承载 继重 的数 类函 l4l44 3 ~ 3840 ..... .. .. .. .. .. .. •. .• .. .. .. .. .. .. .. .. .. .. .. •. .• .. •. .• •. •• .• .. .. .. .. .. .. .. .. .. •. •• .• . 术 技 类 术象 技抽 载和 重数 符函 算虚 运纯 l4l34 4 1-5 小结...............................................

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值