/*#include <iostream>
#include <list>
#include <map>
using namespace std;
#define MaxSortPlayer 100
struct PlayerDamageInfo
{
int playerid;
int damage;
int count;
}m_player[MaxSortPlayer];
list<PlayerDamageInfo> m_list;
map<int,int> SortRank;
bool compare(const PlayerDamageInfo &cur,const PlayerDamageInfo &next)
{
if(cur.damage == next.damage)
{
if(cur.count == next.count)
{
return cur.playerid < next.playerid;
}
else
{
return cur.count < next.count;
}
}
else
{
return cur.damage > next.damage;
}
}
void ReSort()
{
//STL list支持bool 如果用Int返回 会崩溃掉
m_list.sort(compare);
int i = 1;
//清空map
SortRank.clear();
//排序好的list 对 map进行排名
//这个map的排序,可以每次下发给客户端展示具体数据
for( list<PlayerDamageInfo>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
SortRank.insert(make_pair(it->playerid,i));
}
}
void Sort(PlayerDamageInfo *info)
{
//第一次排序检查双向链表是否为空,为空的话直接插入
if(m_list.size() == 0)
{
m_list.push_back(*info);
//加入这个map的原因便在于 假如说排行榜内有自己的数据,那么通过map去查找定位替换我在map中的数据
SortRank.insert(make_pair(info->playerid,m_list.size()));
//开始排序了
ReSort();
return ;
}
//如果说这次造成的伤害,小于链表的最末尾的伤害,那么边插入到末尾
if(info->damage < m_list.back().damage)
{
if(m_list.size() <=MaxSortPlayer)
{
m_list.push_back(*info);
SortRank.insert(make_pair(info->playerid,m_list.size()));
ReSort();
return ;
}
}
//检查自己是否在排名以内,如果在定位自己的排名位置
int pos = m_list.size();
if(SortRank.find(info->playerid) != SortRank.end())
{
pos = SortRank[info->playerid];
}
else if(info->damage > m_list.back().damage)
{
m_list.push_back(*info);
ReSort();
return ;
}
else
{
return ;
}
pos--;
list<PlayerDamageInfo>::iterator it = m_list.begin();
while(pos > 0)
{
it++;
pos--;
}
it->damage = info->damage;
it->count = info->count;
ReSort();
}
int main()
{
for(int i = 1; i<MaxSortPlayer; i++)
{
m_player[i].playerid = i;
m_player[i].damage = i;
m_player[i].count = i;
//开始排序了
Sort(&m_player[i]);
}
//展示排序好的链表
for(list<PlayerDamageInfo>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
cout<<it->playerid<<"\t"<<it->damage<<"\t"<<it->count<<endl;
}
system("pause");
return 0;
}*/
#include <list>
#include <map>
using namespace std;
#define MaxSortPlayer 100
struct PlayerDamageInfo
{
int playerid;
int damage;
int count;
}m_player[MaxSortPlayer];
list<PlayerDamageInfo> m_list;
map<int,int> SortRank;
bool compare(const PlayerDamageInfo &cur,const PlayerDamageInfo &next)
{
if(cur.damage == next.damage)
{
if(cur.count == next.count)
{
return cur.playerid < next.playerid;
}
else
{
return cur.count < next.count;
}
}
else
{
return cur.damage > next.damage;
}
}
void ReSort()
{
//STL list支持bool 如果用Int返回 会崩溃掉
m_list.sort(compare);
int i = 1;
//清空map
SortRank.clear();
//排序好的list 对 map进行排名
//这个map的排序,可以每次下发给客户端展示具体数据
for( list<PlayerDamageInfo>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
SortRank.insert(make_pair(it->playerid,i));
}
}
void Sort(PlayerDamageInfo *info)
{
//第一次排序检查双向链表是否为空,为空的话直接插入
if(m_list.size() == 0)
{
m_list.push_back(*info);
//加入这个map的原因便在于 假如说排行榜内有自己的数据,那么通过map去查找定位替换我在map中的数据
SortRank.insert(make_pair(info->playerid,m_list.size()));
//开始排序了
ReSort();
return ;
}
//如果说这次造成的伤害,小于链表的最末尾的伤害,那么边插入到末尾
if(info->damage < m_list.back().damage)
{
if(m_list.size() <=MaxSortPlayer)
{
m_list.push_back(*info);
SortRank.insert(make_pair(info->playerid,m_list.size()));
ReSort();
return ;
}
}
//检查自己是否在排名以内,如果在定位自己的排名位置
int pos = m_list.size();
if(SortRank.find(info->playerid) != SortRank.end())
{
pos = SortRank[info->playerid];
}
else if(info->damage > m_list.back().damage)
{
m_list.push_back(*info);
ReSort();
return ;
}
else
{
return ;
}
pos--;
list<PlayerDamageInfo>::iterator it = m_list.begin();
while(pos > 0)
{
it++;
pos--;
}
it->damage = info->damage;
it->count = info->count;
ReSort();
}
int main()
{
for(int i = 1; i<MaxSortPlayer; i++)
{
m_player[i].playerid = i;
m_player[i].damage = i;
m_player[i].count = i;
//开始排序了
Sort(&m_player[i]);
}
//展示排序好的链表
for(list<PlayerDamageInfo>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
cout<<it->playerid<<"\t"<<it->damage<<"\t"<<it->count<<endl;
}
system("pause");
return 0;
}*/