今天写包裹中物品排序,有一些需要注意的问题和效率上的需要注意的事情,特此记下,备忘:
我的方法是:
1..遍历包裹,将需要叠加的物品叠加
2..再用qsort进行排序 (这个方法非常的好用,屡试不爽啊,他能将需要排序的数组重写数据.hoho~~~)
排序原则:
先判断物品类型->再判断物品ID->再判断物品拥有数量
需要注意的问题是:防止用户重复点击 一直排序
解决方法:只有服务器回复排序完成之后,客户端才能向服务器发送排序消息
来段测试的小代码
// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <Windows.h>
using namespace std;



struct stElement


{
int nType;
int nCnt;
stElement( int type, int cnt )

{
nType = type;
nCnt = cnt;
}
};


int compare( const void *arg1, const void *arg2 )


{
stElement* p1 = (stElement*)arg1;
stElement* p2 = (stElement*)arg2;


if ( p1->nType < p2->nType )
{
return -1;

}else if ( p1->nType > p2->nType)
{
return 1;

}else
{
if ( p1->nCnt < p2->nCnt )
return 1;
else if ( p1->nCnt > p2->nCnt )
return -1;
else
return 0;
}
}

int _tmain(int argc, _TCHAR* argv[])


{
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) );

qsort( &vec[0], vec.size(), sizeof(stElement), compare );


return 0;
}


posted on 2010-12-18 18:49
风轻云淡 阅读(2053)
评论(7) 编辑 收藏 引用 所属分类:
UI