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

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

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


// 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
		{
			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<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;
}


运行结果:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值