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() )就可.

以上内容取自帖子:如何用std::sort对自定义结构的vector以关键字来排序-CSDN论坛

另一篇参考地址:vector元素为自定义结构体类型时如何对容器元素进行排序?_浮城大亨的专栏-CSDN博客_vector 自定义排序

以下取自帖子:stl vector排序_guang11cheng的专栏-CSDN博客_stl vector 排序

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

方法1:重载运算符

 
  1. #include <vector>

  2. #include <algorithm>

  3. #include <functional>

  4. using namespace std;

  5. struct TItem

  6. {

  7. int m_i32Type;

  8. int m_i32ID;

  9. bool operator <(const TItem& rhs) const // 升序排序时必须写的函数

  10. {

  11. return m_i32Type < rhs.m_i32Type;

  12. }

  13. bool operator >(const TItem& rhs) const // 降序排序时必须写的函数

  14. {

  15. return m_i32Type > rhs.m_i32Type;

  16. }

  17. };

  18. int main()

  19. {

  20. vector<TItem> stItemVec;

  21. TItem stItem1;

  22. stItem1.m_i32Type = 1;

  23. stItem1.m_i32ID = 1;

  24. TItem stItem2;

  25. stItem2.m_i32Type = 2;

  26. stItem2.m_i32ID = 2;

  27. TItem stItem3;

  28. stItem3.m_i32Type = 3;

  29. stItem3.m_i32ID = 3;

  30. TItem stItem4;

  31. stItem4.m_i32Type = 2;

  32. stItem4.m_i32ID = 4;

  33. stItemVec.push_back(stItem1);

  34. stItemVec.push_back(stItem2);

  35. stItemVec.push_back(stItem3);

  36. stItemVec.push_back(stItem4);

  37. // 升序排序

  38. sort(stItemVec.begin(), stItemVec.end(), less<TItem>());

  39. // 或者sort(ctn.begin(), ctn.end()); 默认情况为升序

  40. for (size_t i = 0; i < stItemVec.size(); i++)

  41. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

  42. printf("--\n");

  43. // 降序排序

  44. sort(stItemVec.begin(), stItemVec.end(), greater<TItem>());

  45. for (size_t i = 0; i < stItemVec.size(); i++)

  46. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

  47. return 0;

  48. }

  方法2:全局的比较函数

 
  1. #include <vector>

  2. #include <algorithm>

  3. #include <functional>

  4. using namespace std;

  5. struct TItem

  6. {

  7. int m_i32Type;

  8. int m_i32ID;

  9. };

  10. bool lessmark(const TItem& stItem1, const TItem& stItem2)

  11. {

  12. return stItem1.m_i32Type < stItem2.m_i32Type;

  13. }

  14. bool greatermark(const TItem& stItem1, const TItem& stItem2)

  15. {

  16. return stItem1.m_i32Type > stItem2.m_i32Type;

  17. }

  18. int main()

  19. {

  20. vector<TItem> stItemVec;

  21. TItem stItem1;

  22. stItem1.m_i32Type = 1;

  23. stItem1.m_i32ID = 1;

  24. TItem stItem2;

  25. stItem2.m_i32Type = 2;

  26. stItem2.m_i32ID = 2;

  27. TItem stItem3;

  28. stItem3.m_i32Type = 3;

  29. stItem3.m_i32ID = 3;

  30. TItem stItem4;

  31. stItem4.m_i32Type = 2;

  32. stItem4.m_i32ID = 4;

  33. stItemVec.push_back(stItem1);

  34. stItemVec.push_back(stItem2);

  35. stItemVec.push_back(stItem3);

  36. stItemVec.push_back(stItem4);

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

  38. for (size_t i = 0; i < stItemVec.size(); i++)

  39. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

  40. printf("--\n");

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

  42. for (size_t i = 0; i < stItemVec.size(); i++)

  43. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

  44. return 0;

  45. }

方法3:函数对象

 
  1. #include <vector>

  2. #include <algorithm>

  3. #include <functional>

  4. using namespace std;

  5. struct TItem

  6. {

  7. int m_i32Type;

  8. int m_i32ID;

  9. };

  10. class CompLess

  11. {

  12. public:

  13. bool operator ()(const TItem& stItem1, const TItem& stItem2)

  14. {

  15. return stItem1.m_i32Type < stItem2.m_i32Type;

  16. }

  17. };

  18. class CompGreater

  19. {

  20. public:

  21. bool operator ()(const TItem& stItem1, const TItem& stItem2)

  22. {

  23. return stItem1.m_i32Type > stItem2.m_i32Type;

  24. }

  25. };

  26. int main()

  27. {

  28. vector<TItem> stItemVec;

  29. TItem stItem1;

  30. stItem1.m_i32Type = 1;

  31. stItem1.m_i32ID = 1;

  32. TItem stItem2;

  33. stItem2.m_i32Type = 2;

  34. stItem2.m_i32ID = 2;

  35. TItem stItem3;

  36. stItem3.m_i32Type = 3;

  37. stItem3.m_i32ID = 3;

  38. TItem stItem4;

  39. stItem4.m_i32Type = 2;

  40. stItem4.m_i32ID = 4;

  41. stItemVec.push_back(stItem1);

  42. stItemVec.push_back(stItem2);

  43. stItemVec.push_back(stItem3);

  44. stItemVec.push_back(stItem4);

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

  46. for (size_t i = 0; i < stItemVec.size(); i++)

  47. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

  48. printf("--\n");

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

  50. for (size_t i = 0; i < stItemVec.size(); i++)

  51. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);

  52. return 0;

  53. }

  54. /*

  55. 结果如下:

  56. type: 1, id: 1

  57. type: 2, id: 2

  58. type: 2, id: 4

  59. type: 3, id: 3

  60. --

  61. type: 3, id: 3

  62. type: 2, id: 2

  63. type: 2, id: 4

  64. type: 1, id: 1

  65. 可以看出vector的sort的稳定的。

  66. */

问题:

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

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

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

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

(1)全局的比较函数

 
  1. #include <vector>

  2. #include <algorithm>

  3. #include <functional>

  4. using namespace std;

  5. struct TItem

  6. {

  7. int m_i32Type;

  8. int m_i32ID;

  9. };

  10. bool CompLess(const TItem* pstItem1, const TItem* pstItem2)

  11. {

  12. return pstItem1->m_i32Type < pstItem2->m_i32Type;

  13. }

  14. bool CompGreater(const TItem* pstItem1, const TItem* pstItem2)

  15. {

  16. return pstItem1->m_i32Type > pstItem2->m_i32Type;

  17. }

  18. int main()

  19. {

  20. vector<TItem*> stItemVec;

  21. TItem stItem1;

  22. stItem1.m_i32Type = 1;

  23. stItem1.m_i32ID = 1;

  24. TItem stItem2;

  25. stItem2.m_i32Type = 2;

  26. stItem2.m_i32ID = 2;

  27. TItem stItem3;

  28. stItem3.m_i32Type = 3;

  29. stItem3.m_i32ID = 3;

  30. TItem stItem4;

  31. stItem4.m_i32Type = 2;

  32. stItem4.m_i32ID = 4;

  33. stItemVec.push_back(&stItem1);

  34. stItemVec.push_back(&stItem2);

  35. stItemVec.push_back(&stItem3);

  36. stItemVec.push_back(&stItem4);

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

  38. for (size_t i = 0; i < stItemVec.size(); i++)

  39. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);

  40. printf("--\n");

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

  42. for (size_t i = 0; i < stItemVec.size(); i++)

  43. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);

  44. return 0;

  45. }

(2)函数对象

 
  1. #include <vector>

  2. #include <algorithm>

  3. #include <functional>

  4. using namespace std;

  5. struct TItem

  6. {

  7. int m_i32Type;

  8. int m_i32ID;

  9. };

  10. class CompLess

  11. {

  12. public:

  13. bool operator ()(const TItem* pstItem1, const TItem* pstItem2)

  14. {

  15. return pstItem1->m_i32Type < pstItem2->m_i32Type;

  16. }

  17. };

  18. class CompGreater

  19. {

  20. public:

  21. bool operator ()(const TItem* pstItem1, const TItem* pstItem2)

  22. {

  23. return pstItem1->m_i32Type > pstItem2->m_i32Type;

  24. }

  25. };

  26. int main()

  27. {

  28. vector<TItem*> stItemVec;

  29. TItem stItem1;

  30. stItem1.m_i32Type = 1;

  31. stItem1.m_i32ID = 1;

  32. TItem stItem2;

  33. stItem2.m_i32Type = 2;

  34. stItem2.m_i32ID = 2;

  35. TItem stItem3;

  36. stItem3.m_i32Type = 3;

  37. stItem3.m_i32ID = 3;

  38. TItem stItem4;

  39. stItem4.m_i32Type = 2;

  40. stItem4.m_i32ID = 4;

  41. stItemVec.push_back(&stItem1);

  42. stItemVec.push_back(&stItem2);

  43. stItemVec.push_back(&stItem3);

  44. stItemVec.push_back(&stItem4);

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

  46. for (size_t i = 0; i < stItemVec.size(); i++)

  47. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);

  48. printf("--\n");

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

  50. for (size_t i = 0; i < stItemVec.size(); i++)

  51. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);

  52. return 0;

  53. }

推荐使用函数对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值