unordered_map 自定义键值类型

C++中的map、unordered_map 排序

对于key的自定义排序

#include<bits/stdc++.h>
using namespace std;
//对于map中的key进行paixu
 
struct rule{
	bool operator()(string a,string b){
		return a>b;//对于键是string型按照从大到小排
	}
};
 
int main()
{
	//map中的第三个参数其实就是排序规则,之前不写就会默认成默认排序
	map<string,int,rule>m;
	m["asas"]=199;
	m["zx"]=99;
	m["gsgus"]=878;
	m["yuy"]=1515;
	map<string,int,rule>::iterator it;
	for(it=m.begin();it!=m.end();it++){
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

key是结构体的排序

class Person {
public:
	string name;
	int age;
	Person(string n, int a) :name(n), age(a) {}

	bool operator<(const Person a)const {
		if( age != a.age)
			return age > a.age;//对于键是string型按照从大到小排
		return name > a.name;
	}

};
 
int main()
{
	map<Person, int> mp;
	Person aa("Mark", 16);
	mp[aa] = 40561;
	mp[Person("Andrew", 17)] = 40562;
	mp[Person("Brown", 17)] = 40562;

	for (auto i : mp)
		cout << i.first.name << endl;

	//Brown
	//	Andrew
	//	Mark

map对于value(值)排序

在map中的排序是基于按照key来排序的,所以无法对value直接进行排序,如果想对value进行排序,需要用到vector容器以及sort函数、仿函数,其实也是一套模板而已。

#include<bits/stdc++.h>
using namespace std;
//map中对于value排序
//之前说的map是个键值对,所以需要vector
//来接收的话,那么就需要一对一,就需要用到pair了
 
bool cmp(const pair<string,int> a,pair<string,int>b){
	return a.second>b.second;
}
 
int main()
{
	map<string,int>m;
	m["asas"]=18;
	m["ioio"]=90;
	m["cj"]=89;
	vector<pair<string,int>>v(m.begin(),m.end());
	sort(v.begin(),v.end(),cmp);
	map<string,int>::iterator it;
	for(int i=0;i<v.size();i++){
		cout<<v[i].first<<" "<<v[i].second<<endl;
	}
	return 0;
}

key是结构体,map对于value(值)排序

class Person {
public:
	string name;
	int age;
	Person(string n, int a) :name(n), age(a) {}

	bool operator<(const Person a)const {
		if( age != a.age)
			return age > a.age;//对于键是string型按照从大到小排
		return name > a.name;
	}

};

struct cmp {
	bool operator()(pair<Person,int> m, pair<Person, int> n) {
		return m.second>n.second;
	}
};

int main(){
	map<Person, int> mp;
	Person aa("Mark", 16);
	mp[aa] = 40561;
	mp[Person("Andrew", 17)] = 40562;
	mp[Person("Brown", 17)] = 40562;

	vector<pair<Person, int>> vec(mp.begin(), mp.end());
	sort(vec.begin(), vec.end(), cmp());

	auto i = vec.begin();
	for (int j = 0; j < vec.size();++j) {
		cout <<vec[j].first.name<< " " << vec[j].second << endl;
	}
	//	Brown 40562
	//	Andrew 40562
	//	Mark 40561
}

unordered_map 自定义key类型 对于value(值)排序

unordered_map的参数构成


template<class Key,
    class Ty,
    class Hash = std::hash<Key>,
    class Pred = std::equal_to<Key>,
    class Alloc = std::allocator<std::pair<const Key, Ty> > >
    class unordered_map;
    > class unordered_map
    

第1个参数,存储key值。

第2个参数,存储mapped value。

第3个参数,为哈希函数对象。它将key作为参数,并利用函数对象中的哈希函数(hash function)返回类型为size_t的唯一哈希值。默认值为std::hash。函数对象本质上就是用操作符的类,其对象常称为函数对象,它们是行为类似函数的对象,表现出一个函数的特征,就是通过“对象名+(参数列表)”的方式使用一个类,实质是对operator()操作符的重载。除了可以是函数对象(function object),它也可以是指向函数的指针(这句话是c++官网原话,但是个人认为这两个本质上是一种东西,参照方法1)。

第4个参数,为等式操作符(operator==())重载函数对象。它利用等式操作符来判断两个key是否相等,返回值为bool类型。默认值是std::equal_to。同样除了可以是重载函数对象,它也可以是指向函数的指针。在unordered_map中,任意两个元素之间始终返回false。

所以综上所述,如果我们想要让unordered_map使用自定义的key,那么我们需要做两件事情:

定义哈希函数对象或者指向该函数的指针
定义重载等式操作符operator==的函数对象或者指向该函数的指针

#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>
using namespace std;

class Person{
public:
    string name;
    int age;

    Person(string n, int a){
        name = n;
        age = a;
    }

    bool operator==(const Person & p) const 
    {
        return name == p.name && age == p.age;
    }
};


struct hash_name{
    size_t operator()(const Person & p) const{
        return hash<string>()(p.name) ^ hash<int>()(p.age);
    }
};

int main(int argc, char* argv[]){
    unordered_map<Person, int, hash_name> ids;
    ids[Person("Mark", 17)] = 40561;
    ids[Person("Andrew",16)] = 40562;
    for ( auto ii = ids.begin() ; ii != ids.end() ; ii++ )
        cout << ii->first.name 
        << " "
        << ii->first.age
        << " : "
        << ii->second
        << endl;
    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值