C++ 中list容器,自定义sort排序规则,stl中sort自定义排序规则
来源:互联网 发布:知乎日报接口 编辑:程序博客网 时间:2024/06/05 03:03
最近做游戏开发,其中容器(背包,仓库)中的整理功能需要对容器中的所有道具按照一定的规则来进行整理和排序,
这里有两种解决方案,一是重载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;
- }
- C++ 中list容器,自定义sort排序规则,stl中sort自定义排序规则
- C++ 中list容器,自定义sort排序规则,stl中sort自定义排序规则
- C++ 中list容器,stl中sort自定义排序规则 注意事项
- 自定义规则 Collections.sort() 对 List 排序
- List sort()自定义排序
- c++中sort, priority_queue自有类型和自定义类型的排序规则
- Java中自定义排序规则
- C++ 中sort 函数及 cmp 自定义规则的使用
- C++ 中sort 函数及 cmp 自定义规则的使用
- c++STL sort()排序
- 【JavaScript】数组sort()排序规则
- C# List.Sort()自定义比较方法排序
- list sort()给自定义对象排序
- [STL] List 中sort为什么采用归并排序
- STL LIST中自定义排序函数例子
- sort自定义类型排序
- 自定义sort排序方法
- Sort自定义排序
- ABAP 表格控制(Table Control)和 步循环(STEP-LOOP)
- SilkTest天龙八部系列4-ChildWin
- 鸟哥的私房菜之指令输入与基本指令
- 广播地址计算
- 23种设计模式
- C++ 中list容器,自定义sort排序规则,stl中sort自定义排序规则
- 几个通常用的linux指令
- 【java生成二维码方法事例】
- 百度云网盘 360云盘 金山快盘 等 + Git GUI 实现代码版本管理-个人篇
- Android应用程序开发以及背后的设计思想深度剖析
- java基础复习 多线程
- hadoop集群经常性节点不能连接问题
- 黑马程序员————面向对象1(day5)
- jquery mini ui不错的一个WEB界面脚本