利用仿函数使list中的成员按成员属性排序

  1. // SortTest.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <time.h>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <list>
  8. #include <string>
  9. #include <algorithm>
  10. #include <map>
  11. #include <iterator>
  12. using namespace std;  
  13. class RevFaxTask
  14. {
  15. public:
  16.     std::string                         m_RevDateTime;
  17. };
  18. class CoreUserBox
  19. {
  20. public:
  21.     
  22.     
  23.     std::list < RevFaxTask * >      m_list;
  24.          void CoreUserBox::pushback(RevFaxTask * p_task)
  25.         {
  26.       m_list.push_back(p_task);
  27.         }
  28. };
  29. template<>
  30. struct std::less<RevFaxTask*>
  31. {
  32.     bool operator()( RevFaxTask*_X,  RevFaxTask* _Y) const
  33.     {
  34.         return (_X->m_RevDateTime < _Y->m_RevDateTime);
  35.     }
  36. };
  37. void TestOut(RevFaxTask* p)
  38. {
  39.     cout << p->m_RevDateTime << endl;
  40. }
  41. class   find_func   
  42. {  
  43.      public:  
  44.         ~find_func() {}
  45.          find_func(string val) : m_val(val) {}
  46.          bool   operator()(RevFaxTask *arg)  
  47.         {  
  48.             if   (   arg->m_RevDateTime >  m_val   )  
  49.              return   true;  
  50.              return   false;  
  51.         } 
  52.      private:
  53.                find_func();
  54.                string m_val;
  55.   };
  56. int main(int argc, _TCHAR* argv[])
  57. {
  58.     char buf[64];
  59.     
  60.     std::list<RevFaxTask *>::iterator pos;
  61.     std::list<RevFaxTask *>::iterator pos1;
  62.     std::list<RevFaxTask *>::iterator pos2;
  63.     std::list<RevFaxTask *>::iterator pos3;
  64.     CoreUserBox testList;
  65.     
  66.     RevFaxTask task[50];
  67.      
  68.     
  69.     for(int i = 0; i<50; i++)
  70.     {
  71.         int year = 1899;
  72.         int monthNum = 0;
  73.         int dayNum = 0;
  74.         int hour = 0;
  75.         int minu = 0;
  76.         int sec  = 0;
  77.         while(year < 1900)
  78.         {
  79.             year = rand()%2099;
  80.         }
  81.         while(monthNum == 0)
  82.         {
  83.             monthNum = rand()%12;
  84.         }
  85.         while(dayNum == 0)
  86.         {
  87.             dayNum = rand()%31;
  88.         }
  89.         
  90.         
  91.             hour = rand()%24;
  92.         
  93.             minu = rand()%60;
  94.         
  95.             sec= rand()%60;
  96.                     
  97.         sprintf(buf,
  98.             "%d-%02d-%02d %02d:%02d:%02d",
  99.             year,
  100.             monthNum,
  101.             dayNum,
  102.             hour,
  103.             minu,
  104.             sec);
  105.         
  106.         task[i].m_RevDateTime.assign(buf);
  107.         testList.pushback(&task[i]);
  108.     
  109.     }
  110.  long beginTime =clock();//获得开始时间,单位为毫秒
  111.       
  112.       
  113.     
  114.     testList.m_list.sort(less<RevFaxTask *>());
  115.     
  116.      for_each(testList.m_list.begin(), testList.m_list.end(), TestOut);
  117.       
  118.       
  119.     
  120.      for_each(testList.m_list.begin(), testList.m_list.end(), TestOut);
  121.      pos1 = find_if(testList.m_list.begin(), testList.m_list.end(),find_func("1993-12-14 14:00:00"));
  122.      pos2 = find_if(pos1, testList.m_list.end(),find_func("2089-12-14 14:00:00"));
  123.     
  124.      int iCount1 = 0;
  125.      std::list<RevFaxTask *>::difference_type iCount = distance(pos1,pos2);
  126.      _Distance(pos1,pos2,iCount1);
  127.      
  128.     
  129.     
  130. }

前段时间在学习了点STL的皮毛,某天接到个上级下达的命令,要我写一个算法,实现list表中的成员按其成员属性排序..遂翻书,发现sort函数,但没发现针对表成员属性再排序,但发现个sort支持自定义的operation Function做为参数传入,即自定义排序准则,便自己按普通函数格式写了个,compile,error...not success.上网查阅资料,找到了方法,写出关键部分,以备以后参考:

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值