zoukankan      html  css  js  c++  java
  • STL中sort排序算法第三个参数_Compare的实现本质

    关于C++ STL vector 中的sort排序算法有三种自定义实现,它们本质上都是返回bool类型,提供给sort函数作为第三个参数。

    • 重载运算符
    • 全局的比较函数
    • 函数对象

    我认为从实现方式看,重载运算符和函数对象实现本质上是一样的:两者都是括号运算符的重载。

    • 重载运算符利用了泛型模板,先重载模板中的括号运算符,接着重载里面的大于小于操作符;
    • 而函数对象则是直接针对自己的对象重载括号运算符。

    下图是其中一个泛型模板比较函数,位于头文件stl_function.h中。

     以下是全部代码样例(代码来自http://blog.csdn.net/aastoneaa/article/details/8471722):

      1 //本程序为sort排序实现,方法一:重载运算符 方法二:全局的比较函数 方法三:函数对象
      2 //参考http://blog.csdn.net/aastoneaa/article/details/8471722
      3 
      4 //我认为从实现方式看,重载运算符和函数对象实现本质上是一样的:两者都是括号运算符的重载;
      5 //重载运算符利用了泛型模板,再重载模板中的括号运算福,接着重载里面的大于小于操作符;
      6 //而函数对象则是直接针对自己的对象重载括号运算符。
      7 
      8 #include <iostream>
      9 #include <vector>
     10 #include <algorithm>
     11 
     12 using namespace std;
     13 
     14 
     15 //重载运算符
     16 struct TItem
     17 {
     18     int m_i32Type;
     19     int m_i32ID;
     20 
     21     bool operator <(const TItem& rhs) const // 升序排序时必须写的函数
     22     {
     23         return m_i32Type < rhs.m_i32Type;
     24     }
     25     bool operator >(const TItem& rhs) const // 降序排序时必须写的函数
     26     {
     27         return m_i32Type > rhs.m_i32Type;
     28     }
     29 };
     30 int main()
     31 {
     32     vector<TItem> stItemVec;
     33 
     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     // 升序排序
     57     sort(stItemVec.begin(), stItemVec.end(), less<TItem>());
     58     // 或者sort(ctn.begin(), ctn.end());   默认情况为升序
     59 
     60     for (size_t i = 0; i < stItemVec.size(); i++)
     61         printf("type: %d, id: %d
    ", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
     62 
     63     printf("--
    ");
     64 
     65     // 降序排序
     66     sort(stItemVec.begin(), stItemVec.end(), greater<TItem>());
     67 
     68     for (size_t i = 0; i < stItemVec.size(); i++)
     69         printf("type: %d, id: %d
    ", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
     70 
     71     return 0;
     72 }
     73 
     74 
     75 #if 0
     76 //全局比较函数
     77 struct TItem
     78  {
     79      int m_i32Type;
     80      int m_i32ID;
     81  };
     82 
     83 
     84 bool lessmark(const TItem& stItem1, const TItem& stItem2)
     85  {
     86      return stItem1.m_i32Type < stItem2.m_i32Type;//这里是按照关键字m_i32Type排序,也可以按照关键字m_i32ID排序
     87  }
     88 
     89 
     90 bool greatermark(const TItem& stItem1, const TItem& stItem2)
     91  {
     92      return stItem1.m_i32Type > stItem2.m_i32Type;//这里是按照关键字m_i32Type排序,也可以按照关键字m_i32ID排序
     93  }
     94 
     95 
     96 int main()
     97  {
     98      vector<TItem> stItemVec;
     99 
    100 
    101     TItem stItem1;
    102      stItem1.m_i32Type = 1;
    103      stItem1.m_i32ID = 1;
    104 
    105 
    106     TItem stItem2;
    107      stItem2.m_i32Type = 2;
    108      stItem2.m_i32ID = 2;
    109 
    110 
    111     TItem stItem3;
    112      stItem3.m_i32Type = 3;
    113      stItem3.m_i32ID = 3;
    114 
    115 
    116     TItem stItem4;
    117      stItem4.m_i32Type = 2;
    118      stItem4.m_i32ID = 4;
    119 
    120 
    121     stItemVec.push_back(stItem1);
    122      stItemVec.push_back(stItem2);
    123      stItemVec.push_back(stItem3);
    124      stItemVec.push_back(stItem4);
    125 
    126 
    127     sort(stItemVec.begin(), stItemVec.end(), lessmark); //升序排序
    128 
    129 
    130     for (size_t i = 0; i < stItemVec.size(); i++)
    131          printf("type: %d, id: %d
    ", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
    132 
    133 
    134     printf("--
    ");
    135 
    136 
    137     sort(stItemVec.begin(), stItemVec.end(), greatermark); //降序排序
    138 
    139 
    140     for (size_t i = 0; i < stItemVec.size(); i++)
    141          printf("type: %d, id: %d
    ", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
    142 
    143 
    144     return 0;
    145  }
    146 #endif
    147 
    148 #if 0
    149 //函数对象
    150 struct TItem
    151  {
    152      int m_i32Type;
    153      int m_i32ID;
    154  };
    155 
    156 
    157 class CompLess
    158  {
    159  public:
    160      bool operator ()(const TItem& stItem1, const TItem& stItem2)   //本质上是括号运算符的重载
    161      {
    162          return stItem1.m_i32Type < stItem2.m_i32Type;
    163      }
    164  };
    165 
    166 
    167 class CompGreater
    168  {
    169  public:
    170      bool operator ()(const TItem& stItem1, const TItem& stItem2)
    171      {
    172          return stItem1.m_i32Type > stItem2.m_i32Type;
    173      }
    174  };
    175 
    176 
    177 int main()
    178  {
    179      vector<TItem> stItemVec;
    180 
    181 
    182     TItem stItem1;
    183      stItem1.m_i32Type = 1;
    184      stItem1.m_i32ID = 1;
    185 
    186 
    187     TItem stItem2;
    188      stItem2.m_i32Type = 2;
    189      stItem2.m_i32ID = 2;
    190 
    191 
    192     TItem stItem3;
    193      stItem3.m_i32Type = 3;
    194      stItem3.m_i32ID = 3;
    195 
    196 
    197     TItem stItem4;
    198      stItem4.m_i32Type = 2;
    199      stItem4.m_i32ID = 4;
    200 
    201 
    202     stItemVec.push_back(stItem1);
    203      stItemVec.push_back(stItem2);
    204      stItemVec.push_back(stItem3);
    205      stItemVec.push_back(stItem4);
    206 
    207 
    208     sort(stItemVec.begin(), stItemVec.end(), CompLess()); //升序排序
    209 
    210 
    211     for (size_t i = 0; i < stItemVec.size(); i++)
    212          printf("type: %d, id: %d
    ", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
    213 
    214 
    215     printf("--
    ");
    216 
    217 
    218     sort(stItemVec.begin(), stItemVec.end(), CompGreater()); //降序排序
    219 
    220 
    221     for (size_t i = 0; i < stItemVec.size(); i++)
    222          printf("type: %d, id: %d
    ", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
    223 
    224     return 0;
    225  }
    226 #endif
    View Code
    『注:本文来自博客园“小溪的博客”,若非声明均为原创内容,请勿用于商业用途,转载请注明出处http://www.cnblogs.com/xiaoxi666/』
  • 相关阅读:
    HDU 1856 More is better
    并查集模板
    HDU 1325 Is It A Tree?
    HDU 1272 小希的迷宫
    CodeVS 2639 约会计划
    POJ 1163 数字三角形
    HDU 1232 畅通工程
    HDU 1213 How Many Tables
    树形结构打印二叉树
    网址收藏
  • 原文地址:https://www.cnblogs.com/xiaoxi666/p/6424726.html
Copyright © 2011-2022 走看看