std::vector的排序

人比较懒。。。直接上代码:

 

 1  #include  " stdafx.h "
 2  #include  < Windows.h >
 3  #include  < vector >
 4  #include  < algorithm >
 5  #include  < iostream >
 6  #include  < functional >
 7 
 8 
 9  class  xTestElement
10  {
11  public :
12      xTestElement()
13      {
14      }
15       virtual   ~ xTestElement()
16      {
17      }
18       // return whether self's m_value is less then another object's m_value
19       bool   operator   <  ( const  xTestElement &  rhv )  const  
20      {
21           return   this -> m_value  <  rhv.m_value;
22      }
23 
24  public :
25       int  m_value;
26  };
27 
28  class  xTestElementSorter
29  {
30  public :
31       //  Return whether first element is less than the second   
32       bool   operator  () ( const  xTestElement  * a, const  xTestElement  * b)  const   
33      {   
34           return   * <   * b;   
35      }; 
36  };
37 
38  //  Return whether first element is less than the second
39  bool  Testless(xTestElement *  a,xTestElement *  b)
40  {
41       return   * <   * b;
42  }
43 
44  typedef std::vector < xTestElement *>  vTestQueue;
45  typedef vTestQueue::iterator    itTestQueue;
46  vTestQueue   g_vQueues;
47 
48 
49  int  _tmain( int  argc, _TCHAR *  argv[])
50  {
51      xTestElement TestQueue[ 10 ];
52       for  ( int  i =   0  ;i < 10 ;i ++ )
53      {
54          TestQueue[i].m_value  =  rand();
55          std::cout << TestQueue[i].m_value << std::endl;
56          g_vQueues.push_back( & TestQueue[i]);
57      }
58      
59      std::cout << " begin sort :[ " << GetTickCount() << " ]! " << std::endl;
60       // use sort class
61      std::sort(g_vQueues.begin() , g_vQueues.end() , xTestElementSorter());
62       // use sort function
63       // std::sort(g_vQueues.begin() , g_vQueues.end() , Testless);
64      
65      std::cout << " end sort :[ " << GetTickCount() << " ]! " << std::endl;
66 
67       for  (itTestQueue it  =  g_vQueues.begin();it != g_vQueues.end(); ++ it)
68      {
69          std::cout << ( * it) -> m_value << std::endl;
70      }
71      g_vQueues.clear();
72       return   0 ;
73  }
74 
75 

 

 

posted on 2010-03-01 10:00  笑落天狼 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/codingz/archive/2010/03/01/1675424.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值