需要重写sort中的排序unordered_map不能直接排序,需要使用pair存在vector中
#include<iostream>
#include<map>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std
;
bool comp( const pair
<int,int>& a
, const pair
<int,int> &b
){
return a
.second
> b
.second
;
}
int main
(){
unordered_map
<int,int> ump
;
vector
<int> R
={1,2,3,2,2,4,4,1};
for(auto x
:R
){
ump
[x
]++;
}
vector
<pair
<int,int> > b
;
for(auto x
:ump
){
b
.push_back(x
);
}
sort(b
.begin(),b
.end(),comp
);
for(auto x
:b
)
cout
<< x
.first
<<" " <<x
.second
<<" "<<endl
;
}
转载请注明原文地址:https://tech.qufami.com/read-12186.html