unordered_map自定义排序

#include
#include
#include
#include
using namespace std;
int main() {
	unordered_mapm;
	vector> vec;
	for (int i = 0; i < 10; i++) {
		m[i]++;
	}
	//vec.push_back(pair(1, 3));
	//vec.push_back(pair(2, 4));
	//vec.push_back(pair(3, 5));
	//vec.push_back(pair(4, 2));
	//vec.push_back(pair(5, 1));
	for (auto it:m) {
		vec.push_back(it);
	}
	sort(vec.begin(), vec.end(), [](pair&a, pair&b) {return a.first>b.first; });
	for (auto it = vec.begin(); it != vec.end(); it++) {
		cout << it->first<<" "<second << endl;
	}

	return 0;
}
--------------------------------------------------------------/**/或者**
#include
#include
#include
#include
using namespace std;
bool compare_map(const pair&a, const pair&b) {
	return a.first > b.first;
}

int main() {
	unordered_mapm;
	vector> vec;
	for (int i = 0; i < 10; i++) {
		m[i]++;
	}
	//vec.push_back(pair(1, 3));
	//vec.push_back(pair(2, 4));
	//vec.push_back(pair(3, 5));
	//vec.push_back(pair(4, 2));
	//vec.push_back(pair(5, 1));
	for (auto it:m) {
		vec.push_back(it);
	}
	sort(vec.begin(), vec.end(), compare_map);
	for (auto it = vec.begin(); it != vec.end(); it++) {
		cout << it->first<<" "<second << endl;
	}

	return 0;
}

你可能感兴趣的:(c++,算法,哈希算法)