sort/map/unordered_map自定义类型如何构造比较函数

本文介绍了在C++ STL中,如何为自定义类型定义比较函数和hash函数。内容涵盖比较函数的定义、比较类的创建以及如何在自定义类中实现这两个功能。此外,还讲解了hash类的定义和==运算符的重载方法,以确保自定义类型在sort、map和unordered_map等容器中的正确使用。
摘要由CSDN通过智能技术生成

sort: 定义比较函数 / 定义比较类,用比较类定义对象
map: 比较类 / 比较函数在自定义类中提供
unordered_map: hash类的定义、 ==运算符重载
注:/代表或, 、代表并
比较函数

bool compare(const) const

比较类的定义

    struct cmp {
        bool operator()(const ) const
    }; 

hash函数的定义

    struct hashKey {
        size_t operator()(const) const 
    };

具体实现代码

    #include <iostream>
#include <random>
#include <vector>
#include <map>
#include <array>
#include <unordered_map>
#include <algorithm>
#include <random>
#include <functional>
#include <time.h>
#include <string>
using namespace 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值