如果要自己定义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:重载运算符

  1. #include <vector>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. using namespace std;
  6. struct TItem
  7. {
  8. int m_i32Type;
  9. int m_i32ID;
  10.  
  11. bool operator <(const TItem& rhs) const // 升序排序时必须写的函数
  12. {
  13. return m_i32Type < rhs.m_i32Type;
  14. }
  15. bool operator >(const TItem& rhs) const // 降序排序时必须写的函数
  16. {
  17. return m_i32Type > rhs.m_i32Type;
  18. }
  19. };
  20. int main()
  21. {
  22. vector<TItem> stItemVec;
  23.  
  24. TItem stItem1;
  25. stItem1.m_i32Type = 1;
  26. stItem1.m_i32ID = 1;
  27.  
  28. TItem stItem2;
  29. stItem2.m_i32Type = 2;
  30. stItem2.m_i32ID = 2;
  31.  
  32. TItem stItem3;
  33. stItem3.m_i32Type = 3;
  34. stItem3.m_i32ID = 3;
  35.  
  36. TItem stItem4;
  37. stItem4.m_i32Type = 2;
  38. stItem4.m_i32ID = 4;
  39.  
  40. stItemVec.push_back(stItem1);
  41. stItemVec.push_back(stItem2);
  42. stItemVec.push_back(stItem3);
  43. stItemVec.push_back(stItem4);
  44.  
  45. // 升序排序
  46. sort(stItemVec.begin(), stItemVec.end(), less<TItem>());
  47. // 或者sort(ctn.begin(), ctn.end()); 默认情况为升序
  48.  
  49. for (size_t i = 0; i < stItemVec.size(); i++)
  50. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  51.  
  52. printf("--\n");
  53.  
  54. // 降序排序
  55. sort(stItemVec.begin(), stItemVec.end(), greater<TItem>());
  56.  
  57. for (size_t i = 0; i < stItemVec.size(); i++)
  58. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  59.  
  60. return 0;
  61. }

方法2:全局的比较函数

  1. #include <vector>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. struct TItem
  8. {
  9. int m_i32Type;
  10. int m_i32ID;
  11. };
  12.  
  13. bool lessmark(const TItem& stItem1, const TItem& stItem2)
  14. {
  15. return stItem1.m_i32Type < stItem2.m_i32Type;
  16. }
  17.  
  18. bool greatermark(const TItem& stItem1, const TItem& stItem2)
  19. {
  20. return stItem1.m_i32Type > stItem2.m_i32Type;
  21. }
  22.  
  23. int main()
  24. {
  25. vector<TItem> stItemVec;
  26.  
  27. TItem stItem1;
  28. stItem1.m_i32Type = 1;
  29. stItem1.m_i32ID = 1;
  30.  
  31. TItem stItem2;
  32. stItem2.m_i32Type = 2;
  33. stItem2.m_i32ID = 2;
  34.  
  35. TItem stItem3;
  36. stItem3.m_i32Type = 3;
  37. stItem3.m_i32ID = 3;
  38.  
  39. TItem stItem4;
  40. stItem4.m_i32Type = 2;
  41. stItem4.m_i32ID = 4;
  42.  
  43. stItemVec.push_back(stItem1);
  44. stItemVec.push_back(stItem2);
  45. stItemVec.push_back(stItem3);
  46. stItemVec.push_back(stItem4);
  47.  
  48. sort(stItemVec.begin(), stItemVec.end(), lessmark); //升序排序
  49.  
  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.  
  53. printf("--\n");
  54.  
  55. sort(stItemVec.begin(), stItemVec.end(), greatermark); //降序排序
  56.  
  57. for (size_t i = 0; i < stItemVec.size(); i++)
  58. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  59.  
  60. return 0;
  61. }

方法3:函数对象

  1. #include <vector>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. struct TItem
  8. {
  9. int m_i32Type;
  10. int m_i32ID;
  11. };
  12.  
  13. class CompLess
  14. {
  15. public:
  16. bool operator ()(const TItem& stItem1, const TItem& stItem2)
  17. {
  18. return stItem1.m_i32Type < stItem2.m_i32Type;
  19. }
  20. };
  21.  
  22. class CompGreater
  23. {
  24. public:
  25. bool operator ()(const TItem& stItem1, const TItem& stItem2)
  26. {
  27. return stItem1.m_i32Type > stItem2.m_i32Type;
  28. }
  29. };
  30.  
  31. int main()
  32. {
  33. vector<TItem> stItemVec;
  34.  
  35. TItem stItem1;
  36. stItem1.m_i32Type = 1;
  37. stItem1.m_i32ID = 1;
  38.  
  39. TItem stItem2;
  40. stItem2.m_i32Type = 2;
  41. stItem2.m_i32ID = 2;
  42.  
  43. TItem stItem3;
  44. stItem3.m_i32Type = 3;
  45. stItem3.m_i32ID = 3;
  46.  
  47. TItem stItem4;
  48. stItem4.m_i32Type = 2;
  49. stItem4.m_i32ID = 4;
  50.  
  51. stItemVec.push_back(stItem1);
  52. stItemVec.push_back(stItem2);
  53. stItemVec.push_back(stItem3);
  54. stItemVec.push_back(stItem4);
  55.  
  56. sort(stItemVec.begin(), stItemVec.end(), CompLess()); //升序排序
  57.  
  58. for (size_t i = 0; i < stItemVec.size(); i++)
  59. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  60.  
  61. printf("--\n");
  62.  
  63. sort(stItemVec.begin(), stItemVec.end(), CompGreater()); //降序排序
  64.  
  65. for (size_t i = 0; i < stItemVec.size(); i++)
  66. printf("type: %d, id: %d\n", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  67.  
  68. return 0;
  69. }
  70.  
  71. /*
  72. 结果如下:
  73. type: 1, id: 1
  74. type: 2, id: 2
  75. type: 2, id: 4
  76. type: 3, id: 3
  77. --
  78. type: 3, id: 3
  79. type: 2, id: 2
  80. type: 2, id: 4
  81. type: 1, id: 1
  82. 可以看出vector的sort的稳定的。
  83. */

问题:

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

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

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

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

(1)全局的比较函数

  1. #include <vector>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. struct TItem
  8. {
  9. int m_i32Type;
  10. int m_i32ID;
  11. };
  12.  
  13. bool CompLess(const TItem* pstItem1, const TItem* pstItem2)
  14. {
  15. return pstItem1->m_i32Type < pstItem2->m_i32Type;
  16. }
  17.  
  18. bool CompGreater(const TItem* pstItem1, const TItem* pstItem2)
  19. {
  20. return pstItem1->m_i32Type > pstItem2->m_i32Type;
  21. }
  22.  
  23. int main()
  24. {
  25. vector<TItem*> stItemVec;
  26.  
  27. TItem stItem1;
  28. stItem1.m_i32Type = 1;
  29. stItem1.m_i32ID = 1;
  30.  
  31. TItem stItem2;
  32. stItem2.m_i32Type = 2;
  33. stItem2.m_i32ID = 2;
  34.  
  35. TItem stItem3;
  36. stItem3.m_i32Type = 3;
  37. stItem3.m_i32ID = 3;
  38.  
  39. TItem stItem4;
  40. stItem4.m_i32Type = 2;
  41. stItem4.m_i32ID = 4;
  42.  
  43. stItemVec.push_back(&stItem1);
  44. stItemVec.push_back(&stItem2);
  45. stItemVec.push_back(&stItem3);
  46. stItemVec.push_back(&stItem4);
  47.  
  48. sort(stItemVec.begin(), stItemVec.end(), CompLess); //升序排序
  49.  
  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.  
  53. printf("--\n");
  54.  
  55. sort(stItemVec.begin(), stItemVec.end(), CompGreater); //降序排序
  56.  
  57. for (size_t i = 0; i < stItemVec.size(); i++)
  58. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  59.  
  60. return 0;
  61. }

  (2)函数对象

  1. #include <vector>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. struct TItem
  8. {
  9. int m_i32Type;
  10. int m_i32ID;
  11. };
  12.  
  13. class CompLess
  14. {
  15. public:
  16. bool operator ()(const TItem* pstItem1, const TItem* pstItem2)
  17. {
  18. return pstItem1->m_i32Type < pstItem2->m_i32Type;
  19. }
  20. };
  21.  
  22. class CompGreater
  23. {
  24. public:
  25. bool operator ()(const TItem* pstItem1, const TItem* pstItem2)
  26. {
  27. return pstItem1->m_i32Type > pstItem2->m_i32Type;
  28. }
  29. };
  30.  
  31. int main()
  32. {
  33. vector<TItem*> stItemVec;
  34.  
  35. TItem stItem1;
  36. stItem1.m_i32Type = 1;
  37. stItem1.m_i32ID = 1;
  38.  
  39. TItem stItem2;
  40. stItem2.m_i32Type = 2;
  41. stItem2.m_i32ID = 2;
  42.  
  43. TItem stItem3;
  44. stItem3.m_i32Type = 3;
  45. stItem3.m_i32ID = 3;
  46.  
  47. TItem stItem4;
  48. stItem4.m_i32Type = 2;
  49. stItem4.m_i32ID = 4;
  50.  
  51. stItemVec.push_back(&stItem1);
  52. stItemVec.push_back(&stItem2);
  53. stItemVec.push_back(&stItem3);
  54. stItemVec.push_back(&stItem4);
  55.  
  56. sort(stItemVec.begin(), stItemVec.end(), CompLess()); //升序排序
  57.  
  58. for (size_t i = 0; i < stItemVec.size(); i++)
  59. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  60.  
  61. printf("--\n");
  62.  
  63. sort(stItemVec.begin(), stItemVec.end(), CompGreater()); //降序排序
  64.  
  65. for (size_t i = 0; i < stItemVec.size(); i++)
  66. printf("type: %d, id: %d\n", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  67.  
  68. return 0;
  69. }

  

【转】c++中Vector等STL容器的自定义排序的更多相关文章

  1. C++中vector,set,map自定义排序

    一.vector排序 vector支持cmp,就类似数组,可以直接sort. #include <iostream> #include <algorithm> #include ...

  2. C++ STL中vector(向量容器)使用简单介绍

    原文:http://www.seacha.com/article.php/knowledge/cbase/2013/0903/2205.html C++ vector(向量容器)是一个线性顺序结构.相 ...

  3. STL容器——对map排序

    STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...

  4. STL容器set()--->自定义数据类型

    set容器中不能插入重复的元素,需要其插入的元素有比较大小(<).相等(==) 的逻辑判断,这是因为set中的元素是有序排列, 默认从小到大排列 std::set<type,std::le ...

  5. C/C++知识要点2——STL中Vector、Map、Set容器的实现原理

    1.Vector是顺序容器.是一个动态数组.支持随机存取.插入.删除.查找等操作,在内存中是一块连续的空间.在原有空间不够情况下自己主动分配空间.添加为原来的两倍.vector随机存取效率高,可是在v ...

  6. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  7. C++中 vector(容器)的用法

    vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: ...

  8. c++中vector等容器的实现机制

    stl容器区别: vector list deque set map-底层实现 stl容器区别: vector list deque set map (转) 在STL中基本容器有: vector.li ...

  9. 不要在公共接口中传递STL容器

    最近的一个项目,是开发一个framework,提供给公司内部不同的产品线使用. 之间遇到的一个问题,就是STL容器的使用, 而结论是不要在公共接口中传递STL容器: 这里说的STL容器,但主要则是指容 ...

随机推荐

  1. php和egret的配合

    egret对资源路径和js的应用都是相对路径,而在现在许多流行的框架里,一般都把js和资源放到专门的文件夹下,如public. 修改步骤: 1.修改index.html,改为全路径,如: <sc ...

  2. Lua中调用C函数

    Lua利用一个虚拟的堆栈来给C传递值或从C获取值.每当Lua调用C函数,都会获得一个新的堆栈,该堆栈初始包含所有的调用C函数所需要的参数值(Lua传给C函数的调用实参),并且C函数执行完毕后,会把返回 ...

  3. maven pox.xml 设置主入口配置节点

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  4. Nodejs建站笔记-注册登录流程的简单实现

    1. 使用Backbone实现前端hash路由 登录注册页面如下: 初步设想将注册和登录作为两个不同的url实现,但登录和注册功能的差距只有form表单部分,用两个url实现显然开销过大,所以最终方案 ...

  5. Testing - Selenium

    Selenium http://www.seleniumhq.org/ User Guide http://www.seleniumhq.org/docs/ Webdriver中文社区 http:// ...

  6. SQL Server里ORDER BY的歧义性

    在今天的文章里,我想谈下SQL Server里非常有争议和复杂的话题:ORDER BY子句的歧义性. 视图与ORDER BY 我们用一个非常简单的SELECT语句开始. -- A very simpl ...

  7. 文本溢出text-overflow

    文本溢出text-overflow 问题:有一个新闻标题,标题宽度为200px,文字为宋体,加粗,文字大小为16px,颜色为黑色,行高为25px,要求单行显示,并且超出时显示“…”,请按要求完成效果. ...

  8. JS魔法堂:属性、特性,傻傻分不清楚

    一.前言 或许你和我一样都曾经被下面的代码所困扰 var el = document.getElementById('dummy'); el.hello = "test"; con ...

  9. IOS开发UI基础UIControl事件

    UIControl事件1.UIControlEventTouchDown单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候. 2.UIControlEventTouchDownRepeat多点触 ...

  10. C#怎样处理xml文件的大于号和小于号等常用符号(xml符号引发的程序错误)

    在程序中由xml配置而成的sql语句要转换为C#支持的sql语句 <settings> <select> a.*</select> <from> (se ...