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

54 篇文章 0 订阅
3 篇文章 0 订阅

上一篇博客已经简单的介绍了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
输出结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值