您的位置:首页 > 运维架构

【STL】map如何进行排序并且求的TopK

2016-11-15 18:05 281 查看
上一篇博客已经简单的介绍了map的一些简单用法,下面简单分析下算法库中的排序

sort

<algorithm>

template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );


Sort elements in range
Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and
comp for the second.

Elements that would compare equal to each other are not guaranteed to keep their original relative order.

Parameters

first, last Random-Access iterators to the initial and final positions of the sequence to be sorted. The range used is
[first,last), which contains all the elements between first and
last, including the element pointed by first but not the element pointed by
last. comp Comparison function object that, taking two values of the same type than those contained in the range, returns
true if the first argument goes before the second argument in the specific
strict weak ordering it defines, and false otherwise.

Return value

库文件里给出的例子是这个样子的

// sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
vector<int>::iterator it;

// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp
sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

// print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}


参数要求有两个位置,并且有一个排序方式

下面是我写的用sort排序map中的内容

第一种

struct CMPbyVALUE
{
bool operator()(const pair<string,int>&left,const pair<string,int>&right)
{
return left.second>right.second;
}
};
void GetTopKMap1(const int k)
{
vector<pair<string,int>> v;
map<string,int> mp;
map<string,int>::iterator it;
for (int i=0;i<10;i++)
{
mp["苹果"]++;
}
for (int i=0;i<9;i++)
{
mp["梨"]++;
}
for (int i=0;i<8;i++)
{
mp["桃子"]++;
}
for (int i=0;i<7;i++)
{
mp["哈密瓜"]++;
}
for(it=mp.begin();it!=mp.end();it++)
{
v.push_back(make_pair(it->first,it->second));
}
sort(v.begin(),v.end(),CMPbyVALUE());
for(int j=0;j<k;j++)
cout<<v[j].first<<" "<<v[j].second<<endl;
cout<<endl;
}
把map中的内容重新插入到vector中,这里用make_pair(),将内容插入,然后调用sort排序,,并求topk

第二种方法

struct CMP
{
bool operator()(pair<string,int>&left,pair<string,int>&right)
{
return left.second>right.second;
}
};
void GetTopKMap2(vector<pair<string,int>> &v,const int k)
{
map<string,int> mp;
map<string,int>::iterator it=mp.begin();
for (int i=0;i<10;i++)
{
mp["苹果"]++;
}
for (int i=0;i<9;i++)
{
mp["梨"]++;
}
for (int i=0;i<8;i++)
{
mp["桃子"]++;
}
for (int i=0;i<7;i++)
{
mp["哈密瓜"]++;
}

for (it=mp.begin();it!=mp.end();it++)
{
v.push_back(*it);
}
sort(v.begin(),v.end(),CMP());
for (int i=0;i<k;i++)
{
cout<<v[i].first<<" "<<v[i].second<<endl;
}

cout<<endl;
}
这里是将map里的内容用迭代器表示,并在vector中插入迭代器,然后进行sort排序,并求topk

输出结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: