天天看點

C++ 中list容器,自定義sort排序規則,stl中sort自定義排序規則

最近做遊戲開發,其中容器(背包,倉庫)中的整理功能需要對容器中的所有道具按照一定的規則來進行整理和排序,

這裡有兩種解決方案,一是重載list.sort()的操作運算符,二是通過list.sort(greater<Class*>) 指定類似與回調函數的方式來排序。

[cpp] view

plaincopyprint?

// test.cpp : 定義控制台應用程式的入口點。  

//  

#include "stdafx.h"  

#include <list>  

#include <string>  

#include <functional>  

#include <iostream>  

#include <algorithm>  

using namespace std;  

class ItemSort  

{  

public :  

    int _itemType;  

    int _itemQuality;  

    int _itemId;  

    int _itenNum;  

};  

template<> struct std::greater<ItemSort*>  

    bool operator()( ItemSort* _X,  ItemSort* _Y) const // 重載運算符  

    {  

        if (_X->_itemType > _Y->_itemType) // big to small  

        {  

            return true;  

        }else if (_X->_itemType == _Y->_itemType) // to compare next _itemQuality  

            if (_X->_itemQuality > _Y->_itemQuality) // big to small  

            {  

                return true;  

            }  

            else if (_X->_itemQuality = _Y->_itemQuality)// to compare next _itemId  

                if (_X->_itemId > _Y->_itemId) // big to small  

                {  

                    return true;  

                }else if (_X->_itemId == _Y->_itemId)// to compare next _itenNum  

                    if (_X->_itenNum > _Y->_itenNum)// big to small  

                    {  

                        return true;  

                    }else {return false;} // end of _itemNum  

                }else {return false;} // end of _itemId  

            }else{return false;} // end of _itemQuality  

        }else{  return false;}// end of _itemType  

    }  

bool CompareRules(ItemSort* _X,  ItemSort* _Y) // 回調函數  

    if (_X->_itemType > _Y->_itemType) // big to small  

        return true;  

    }else if (_X->_itemType == _Y->_itemType) // to compare next _itemQuality  

        if (_X->_itemQuality > _Y->_itemQuality) // big to small  

        }  

        else if (_X->_itemQuality = _Y->_itemQuality)// to compare next _itemId  

            if (_X->_itemId > _Y->_itemId) // big to small  

            }else if (_X->_itemId == _Y->_itemId)// to compare next _itenNum  

                if (_X->_itenNum > _Y->_itenNum)// big to small  

                }else {return false;} // end of _itemNum  

            }else {return false;} // end of _itemId  

        }else{return false;} // end of _itemQuality  

    }else{  return false;}// end of _itemType  

}  

int main(int argc, char* argv[])  

{   

    list<ItemSort*> mylist;   

    list<ItemSort*>::iterator iter;   

    ItemSort* itemSort = new ItemSort();  

    itemSort->_itemType = 1;  

    itemSort->_itemQuality = 2;  

    itemSort->_itemId = 1;  

    itemSort->_itenNum =1;  

    mylist.push_back(itemSort);  

    ItemSort* itemSort2 = new ItemSort();  

    itemSort2->_itemType = 2;  

    itemSort2->_itemQuality = 2;  

    itemSort2->_itemId = 1;  

    itemSort2->_itenNum =1;  

    mylist.push_back(itemSort2);  

    ItemSort* itemSort3 = new ItemSort();  

    itemSort3->_itemType = 2;  

    itemSort3->_itemQuality = 2;  

    itemSort3->_itemId = 2;  

    itemSort3->_itenNum = 1;  

    mylist.push_back(itemSort3);  

    ItemSort* itemSort4 = new ItemSort();  

    itemSort4->_itemType = 2;  

    itemSort4->_itemQuality = 2;  

    itemSort4->_itemId = 2;  

    itemSort4->_itenNum = 2;  

    mylist.push_back(itemSort4);  

    //mylist.sort(greater<ItemSort*>()); // 重載運算符方式自定義排序規則  

    mylist.sort(CompareRules); // 回調函數方式自定義排序規則  

    for (iter = mylist.begin(); iter != mylist.end();++iter)    

    {       

        cout <<(*iter)->_itemType << " \t" <<(*iter)->_itemQuality<< " \t" <<(*iter)->_itemId<< " \t" <<(*iter)->_itenNum << endl;   

    }   

    getchar();  

    return 0;  

}