今天写包裹中物品排序,有一些需要注意的问题和效率上的需要注意的事情,特此记下,备忘:
我的方法是:
1..遍历包裹,将需要叠加的物品叠加
2..再用qsort进行排序 (这个方法非常的好用,屡试不爽啊,他能将需要排序的数组重写数据.hoho~~~)
排序原则:
先判断物品类型->再判断物品ID->再判断物品拥有数量
需要注意的问题是:防止用户重复点击 一直排序
解决方法:只有服务器回复排序完成之后,客户端才能向服务器发送排序消息
来段测试的小代码
// test.cpp : 定义控制台应用程序的入口点。
//
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
#include <vector>
#include <Windows.h>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct stElement
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int nType;
int nCnt;
stElement( int type, int cnt )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nType = type;
nCnt = cnt;
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int compare( const void *arg1, const void *arg2 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
stElement* p1 = (stElement*)arg1;
stElement* p2 = (stElement*)arg2;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( p1->nType < p2->nType )
{
return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if ( p1->nType > p2->nType)
{
return 1;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
if ( p1->nCnt < p2->nCnt )
return 1;
else if ( p1->nCnt > p2->nCnt )
return -1;
else
return 0;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int _tmain(int argc, _TCHAR* argv[])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
vector<stElement> vec;
vec.push_back( stElement(3,10) );
vec.push_back( stElement(5,10) );
vec.push_back( stElement(1,11) );
vec.push_back( stElement(2,50) );
vec.push_back( stElement(3,9) );
vec.push_back( stElement(3,20) );
vec.push_back( stElement(7,1) );
vec.push_back( stElement(3,8) );
vec.push_back( stElement(4,7) );
vec.push_back( stElement(3,8) );
vec.push_back( stElement(3,5) );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qsort( &vec[0], vec.size(), sizeof(stElement), compare );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2010-12-18 18:49
风轻云淡 阅读(2036)
评论(7) 编辑 收藏 引用 所属分类:
UI