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

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

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


// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
using namespace std;

class ItemSort
{
public :
	int _itemType;
	int _itemQuality;
	int _itemId;
	int _itenNum;
};

template<> struct std::greater
{
	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
		{
			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
}

int main(int argc, char* argv[])
{ 
	list mylist; 
	list::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()); // 重载运算符方式自定义排序规则
	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;
}


运行结果:



你可能感兴趣的:(工作)