当前位置:网站首页>map,set用pari作为key值,如何定义
map,set用pari作为key值,如何定义
2022-06-29 02:46:00 【疯疯癫癫才自由】
对于没有默认的哈希函数的类型,如自定义的 class 类型,pair 类型等,我们就必须自己
指定一个哈希函数。这也是为什么直接构建 pair 类型的 unordered_set 如 unordered_set<pair<int, int>> uset 会出现问题
当然,自己定义的类:
可以在定义结构体(类)的时候就重载运算符,当然当重载的运算符是成员函数时,成员函数
的(显示)参数数量比运算对象的数量少一个,this绑定到左侧运算对象;
当不是成员函数的时候,要定义成友元函数,即在函数前加关键字 friend ,此时友元函数的参数
和一般函数的数量一样
#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_map>
using namespace std;
struct comp
{
int operator()(const std::pair<int, int>& p) const {
return p.first ^ p.second; //返回值为bool,比较符为&,|都比这一个慢,
}
};
/**
这和priority_queue 是有区别的,priority_queue默认值大的优先级高,默认按小于排序,
但是你变成了大于,也就是将小于排变成大于排序,值小的优先级高。
但是这儿不是priority_queue,而是对set或map排序,就和sort的cmp函数的功能一样,
后者都是默认按从小到大的顺序排序,你只是将它的排序顺序给调换了,按从大到小排序,
优先队列,优先队列,自然和一般的容器有所不同。
并且这儿的comp或者temp都只是一个调用对象,比较集合里的两个元素,必然是需要两个参数的,你怎么能和类的成员函数
想到一起呢,是的,开始我是相成了类的成员函数还把thies给写了出来,说明C++学的确实不好,当然,
我只看了C++ primer,并没有系统的去学。
*/
struct temp
{
bool operator()(const std::pair<int, int>& p1,const std::pair<int,int> &p2){
if(p1.first!=p2.first)
return p1.first > p2.first; //返回值为bool,比较符为&,|都比这一个慢,
else
return p1.second > p2.second;
}
};
/**
对于没有默认的哈希函数的类型,如自定义的 class 类型,pair 类型等,我们就必须自己
指定一个哈希函数。这也是为什么直接构建 pair 类型的 unordered_set
如 unordered_set<pair<int, int>> uset 会出现问题
*/
unordered_map<pair<int,int> ,int,comp> ump; //实践上机证明将坐标转化为long long 比这种方式快上一点
int main()
{
set<pair<int,int>, temp>st;
for(int i=1;i<=10;++i)
st.insert({3*i%15,i+4}); //set只能每次插入一个值
for(auto a:st)
cout << a.first << ' ' << a.second << endl;
return 0;
}
边栏推荐
- SQL continuous login problem
- In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
- EMC、EMI、EMS的關系
- Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
- What is a thread pool?
- The linkedhashset set makes the elements orderly without repetition
- 信息学奥赛一本通 1361:产生数(Produce)
- MySQL binlog log cleanup
- Exec function of PHP
- PHP的exec函数
猜你喜欢

"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express

【无标题】

PWN attack and defense world level2

SQL training 01

对补wasm环境的一些测试

解决allegro中测量距离时,点击一个点后光标闪烁的问题

allegro 设计中显示网络飞线或关闭网络飞线的方法

How does sound amplify weak sounds

Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color

Regular expression (?: pattern)
随机推荐
Double click events and click events
18. `bs对象.节点名.next_sibling` 获取兄弟节点
MySQL binlog log cleanup
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
使用gdb添加断点的几种方式
2022年启牛学堂证券开户安全的嘛?
微信小程序安全登录,必须先校验微信返回openid,确保信息唯一性。
Eight difficulties of embedded C language
LabVIEW jump to web page
PWN攻防世界guess_num
Application of fsockopen function
allegro对走好的线取消走线的方法
There is a time delay for the click event on the mobile terminal. What is the delay time? How to solve it?
PMP Business Analysis Overview
【无标题】
信息学奥赛一本通 1361:产生数(Produce)
安装kibana
fsockopen函数的应用
18. ` BS object. Nom du noeud. Suivant Sibling ` get Brother Node
Equal wealth