2007-11
16

实现代码如下,实现思路是开辟新存储区存储临时数据,这一点上来说并无特别之处。强大的地方在于用了多个索引对同一片数据进行快速查找和定位,enjoy~

#include <iostream>
#include <list>
#include <map>
#include <vector>
#include <set>
#include <cmath>
#include <time.h>
#include <algorithm>
#include <limits>


using namespace std;
typedef map<int,double> NormalMap;
typedef map<pair<double,int>,NormalMap::const_iterator>ReverseMap;//pair中的double为排序参考值


vector<int> sortIndex(NormalMap m)//对给定的map的value进行按照升序排序并输出原来的位置
{
vector<int> resV;
ReverseMap n;
for(NormalMap::const_iterator i=m.begin(); i!=m.end(); i++)
{
n[make_pair(i->second,i->first) ] = i;
}
for(ReverseMap::const_iterator i=n.begin(); i!=n.end(); i++)
{
NormalMap::const_iterator j = i->second;
resV.push_back(j->first);
}
return resV;
}

以下是笔者在VS2008beta2上的调试结果(VC6对标准C++支持有一点bug,编译不通过)


RSS feed

评论 立刻发表评论 »

还没有评论。