C++ 中list容器,自定义sort排序规则,stl中sort自定义排序规则

最近做游戏开发,其中容器(背包,仓库)中的整理功能需要对容器中的所有道具按照一定的规则来进行整理和排序,

这里有两种解决方案,一是重载list.sort()的操作运算符,二是通过list.sort(greater<Class*>) 指定类似与回调函数的方式来排序。


  1. // test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. #include "stdafx.h"  
  4. #include <list>  
  5. #include <string>  
  6. #include <functional>  
  7. #include <iostream>  
  8. #include <algorithm>  
  9. using namespace std;  
  10.   
  11. class ItemSort  
  12. {  
  13. public :  
  14.     int _itemType;  
  15.     int _itemQuality;  
  16.     int _itemId;  
  17.     int _itenNum;  
  18. };  
  19.   
  20. template<> struct std::greater<ItemSort*>  
  21. {  
  22.     bool operator()( ItemSort* _X,  ItemSort* _Y) const // 重载运算符  
  23.     {  
  24.         if (_X->_itemType > _Y->_itemType) // big to small  
  25.         {  
  26.             return true;  
  27.         }else if (_X->_itemType == _Y->_itemType) // to compare next _itemQuality  
  28.         {  
  29.             if (_X->_itemQuality > _Y->_itemQuality) // big to small  
  30.             {  
  31.                 return true;  
  32.             }  
  33.             else if (_X->_itemQuality = _Y->_itemQuality)// to compare next _itemId  
  34.             {  
  35.                 if (_X->_itemId > _Y->_itemId) // big to small  
  36.                 {  
  37.                     return true;  
  38.                 }else if (_X->_itemId == _Y->_itemId)// to compare next _itenNum  
  39.                 {  
  40.                     if (_X->_itenNum > _Y->_itenNum)// big to small  
  41.                     {  
  42.                         return true;  
  43.                     }else {return false;} // end of _itemNum  
  44.                 }else {return false;} // end of _itemId  
  45.             }else{return false;} // end of _itemQuality  
  46.         }else{  return false;}// end of _itemType  
  47.     }  
  48. };  
  49.   
  50. bool CompareRules(ItemSort* _X,  ItemSort* _Y) // 回调函数  
  51. {  
  52.     if (_X->_itemType > _Y->_itemType) // big to small  
  53.     {  
  54.         return true;  
  55.     }else if (_X->_itemType == _Y->_itemType) // to compare next _itemQuality  
  56.     {  
  57.         if (_X->_itemQuality > _Y->_itemQuality) // big to small  
  58.         {  
  59.             return true;  
  60.         }  
  61.         else if (_X->_itemQuality = _Y->_itemQuality)// to compare next _itemId  
  62.         {  
  63.             if (_X->_itemId > _Y->_itemId) // big to small  
  64.             {  
  65.                 return true;  
  66.             }else if (_X->_itemId == _Y->_itemId)// to compare next _itenNum  
  67.             {  
  68.                 if (_X->_itenNum > _Y->_itenNum)// big to small  
  69.                 {  
  70.                     return true;  
  71.                 }else {return false;} // end of _itemNum  
  72.             }else {return false;} // end of _itemId  
  73.         }else{return false;} // end of _itemQuality  
  74.     }else{  return false;}// end of _itemType  
  75. }  
  76.   
  77. int main(int argc, char* argv[])  
  78. {   
  79.     list<ItemSort*> mylist;   
  80.     list<ItemSort*>::iterator iter;   
  81.   
  82.     ItemSort* itemSort = new ItemSort();  
  83.     itemSort->_itemType = 1;  
  84.     itemSort->_itemQuality = 2;  
  85.     itemSort->_itemId = 1;  
  86.     itemSort->_itenNum =1;  
  87.     mylist.push_back(itemSort);  
  88.   
  89.     ItemSort* itemSort2 = new ItemSort();  
  90.     itemSort2->_itemType = 2;  
  91.     itemSort2->_itemQuality = 2;  
  92.     itemSort2->_itemId = 1;  
  93.     itemSort2->_itenNum =1;  
  94.     mylist.push_back(itemSort2);  
  95.   
  96.     ItemSort* itemSort3 = new ItemSort();  
  97.     itemSort3->_itemType = 2;  
  98.     itemSort3->_itemQuality = 2;  
  99.     itemSort3->_itemId = 2;  
  100.     itemSort3->_itenNum = 1;  
  101.     mylist.push_back(itemSort3);  
  102.   
  103.     ItemSort* itemSort4 = new ItemSort();  
  104.     itemSort4->_itemType = 2;  
  105.     itemSort4->_itemQuality = 2;  
  106.     itemSort4->_itemId = 2;  
  107.     itemSort4->_itenNum = 2;  
  108.     mylist.push_back(itemSort4);  
  109.   
  110.     //mylist.sort(greater<ItemSort*>()); // 重载运算符方式自定义排序规则  
  111.     mylist.sort(CompareRules); // 回调函数方式自定义排序规则  
  112.     for (iter = mylist.begin(); iter != mylist.end();++iter)    
  113.     {       
  114.         cout <<(*iter)->_itemType << "  " <<(*iter)->_itemQuality<< "  " <<(*iter)->_itemId<< "  " <<(*iter)->_itenNum << endl;   
  115.     }   
  116.     getchar();  
  117.     return 0;  
  118. }  
原文地址:https://www.cnblogs.com/hzcya1995/p/13318804.html