C++笔记:Hash Function 散列函数

news2024/10/6 22:31:34

1. Hash Function 散列函数

  • 简单的Hash实现:
class CustomerHash {
public:
    size_t operator()(const Customer& c) const {
        return hash<std::string>()(c.fname) +  // first name
               hash<std::string>()(c.lname) +  // last name
               hash<long>()(c.no);
        // 返回hash_code
    }
};
  • 针对Customer类对象,直接对其各个成员变量分别调用标准库中的hash函数,并将得到的哈希值相加。

  • 虽然简单,但这种实现方式可能会导致哈希冲突(即不同对象可能产生相同的哈希值)。

  • 改进的Hash实现

class CustomerHash {
public:
		size_t operator()(const Customer& c) const {
				return hash_val(c.frame, c.lname, c.no);
		}
};
template <typename... Type>
inline size_t hash_val(const Type&... args){
		size_t seed = 0;
		hash_val(seed, args...);
		return seed;  //seed最终就被视为hash code
}
  • 顶层主函数:初始化 seed 为0,调用 hash_val递归处理所有参数,并返回最终的哈希值。
template <typename T, typename... Types>
inline void hash_val(size_t& seed, const T& val, const Type&... args){
		hash_combine(seed, val);
		hash_val(seed, args);
};
  • 这是一个递归函数模板,它处理一个或多个参数:
    • 首先,调用 hash_combine函数处理当前参数 val并更新 seed
    • 然后,递归调用自身处理剩余的参数
template <typename T>
inline void hash_val(size_t& seed, const T& val){
		hash_combine(seed, val);
}
  • 这是上面递归函数的终止版本,当没有更多参数时,只处理最后一个参数
#include<functional>
template <typename T>
inline void hash_combine(size_t& seed, const T& val){
		seed ^= hash<T>()(val) + 0x9e3779b9 + (seed<<6) + (seed<<2);
}
  • 结合当前的哈希值和传入的值来更新seed使用了一些常见的哈希组合技巧(例如魔数0x9e3779b9),以减少冲突。

  • 以struct hash 偏特化的形式实现Hash function

    • std 命名空间中特化 hash 结构体以支持 MyString 类型
namespace std {
    template<>
    struct hash<MyString> {
        size_t operator()(const MyString& s) const noexcept {
            return hash<string>()(s.get());
        }
    };
}

2. tuple

  • 构造方式:
// 构造方式1
tuple<int, float, string> t1(41, 6.3, "nico");
cout << "t1: " << get<0>(t1) << " " 
		<< get<1>(t1) << " " << get<2>(t1) << endl;
		
// 构造方式2
auto t2 = make_tuple(22, 44, "stacy");

// 构造方式3
tuple<int,float,string> t3(77, 1.1, "more light");
int i1;
float f1;
stting s1;
tie(i1, f1, s1) = t3;
  • 用例:
tuple<int, float, string> t(41, 6.3, "nico"); // 调用了构造函数
t.head() // 调用了head()函数,返回 41
t.tail() // 调用了tail()函数,返回 tuple<float, string>
t.tail().head() // 调用了tail()然后是head()函数,返回 6.3
t.tail().tail().head() // 调用了两次tail()然后是head()函数,返回 "nico"
&(t.tail()) // 调用了tail()函数,返回 tuple<float, string> 的引用
  • 源码:
// 这是一个变长模板声明,表示tuple类可以接受任意数量和类型的模板参数
template<typename... Values> class tuple;
template<> class tuple<> {};
template<typename Head, typename... Tail>
class tuple<Head, Tail...> : public tuple<Tail...>
{
		typedef tuple<Tail...> inherited;
public:
		tuple() {};
		tuple(Head v, Tail... vtail) : m_head(v), inheriter(vtail...) {}
		
		typename Head::type head() { return m_head;}
		inherited& tail { return *this;}
protected:
		Head m_head;
};
  • tuple<Head, Tail...>递归地继承了tuple<Tail...>,即剩下的参数组成的元组。
    • tuple<int, float, string> : private tuple<float, string>
    • tuple<float, string> : private tuple<string>
    • tuple<string>
  • t.tail()中,调用tail()函数,tail()返回*this。此时,*this指向的对象类型是tuple<int, float, string>,但通过类型转换返回为inherited&,即tuple<float, string>&
  • t.tail() 会返回一个 tuple<float, string>
  • &(t.tail()) 会返回一个tuple<float, string>的引用

3. type traits

 

应用:

template <typename T>
void type_traits_output(const T&x)
{
		cout << "\\n type traits for type:" << typeid(T).name() << endl;
		
		cout << "is_void\\t" << is_void<T>::value << endl; // 0 or 1
		cout << "is_integral\\t" << is_integral<T>::value << endl; // 0 or 1
		...
}

int i = 0;
double d = 0.0;
type_traits_output(i);
type_traits_output(d);

4. type traits中is_void的实现

为什么判断void时需要移除volatile和const限定符?

假设你有一个类型是const voidvolatile void,这些类型本质上还是void类型,只是加了限定符。如果不去除这些限定符,直接判断类型是否为void,判断结果将会是错误的,因为const voidvoid在严格意义上是不同的类型。

通过移除这些限定符,可以确保我们判断的基础类型是void,而不是const voidvolatile void。这确保了类型特性模板的准确性和一致性。

  • 移除volatile限定符:
// 如果类型不带volatile限定符,通用模板直接定义type为原始类型_Tp
template<typename _Tp>
struct remove_volatile {
    typedef _Tp type;
};

// 如果类型带有volatile限定符,特化模板定义会去掉volatile限定符
template<typename _Tp>
struct remove_volatile<_Tp volatile> {
    typedef _Tp type;
};

  • 移除const限定符
template<typename _Tp>
struct remove_const {
    typedef _Tp type;
};

template<typename _Tp>
struct remove_const<_Tp const> {
    typedef _Tp type;
};
  • 移除constvolatile限定符:
template<typename _Tp>
struct remove_cv {
    typedef typename remove_const<typename remove_volatile<_Tp>::type>::type type;
};
  • 判断是否为void 类型:
template<typename>
struct __is_void_helper : public false_type {};

template<>
struct __is_void_helper<void> : public true_type {};
  • 最终的is_void实现
template<typename _Tp>
struct is_void : public __is_void_helper<typename remove_cv<_Tp>::type> {};

5. cout 标准输出流

  • ostream类是标准C++库中的输出流类,定义在<ostream>头文件中。这个类提供了多种运算符重载,以便将不同类型的数据输出到流中
ostream& operator<<(char c) {
    // 输出一个字符
    return *this;
}

ostream& operator<<(unsigned char c) {
    // 输出一个无符号字符
    return *this << static_cast<char>(c);
}

ostream& operator<<(signed char c) {
    // 输出一个有符号字符
    return *this << static_cast<char>(c);
}

ostream& operator<<(const char* s) {
    // 输出一个C风格字符串
    return *this;
}

ostream& operator<<(const unsigned char* s) {
    // 输出一个无符号字符指针,转换为C风格字符串
    return *this << reinterpret_cast<const char*>(s);
}

ostream& operator<<(const signed char* s) {
    // 输出一个有符号字符指针,转换为C风格字符串
    return *this << reinterpret_cast<const char*>(s);
}

ostream& operator<<(const void* p) {
    // 输出一个指针
    return *this;
}

ostream& operator<<(int n) {
    // 输出一个整数
    return *this;
}

ostream& operator<<(unsigned int n) {
    // 输出一个无符号整数
    return *this;
}

ostream& operator<<(long n) {
    // 输出一个长整数
    return *this;
}

ostream& operator<<(unsigned long n) {
    // 输出一个无符号长整数
    return *this;
}
  • cout是一个全局的_IO_ostream_withassign对象,表示标准输出流。其继承自ostream,并提供赋值运算符的重载。
class _IO_ostream_withassign : public ostream {
public:
    _IO_ostream_withassign& operator=(ostream& os) {
        // 实现赋值操作
        // 允许将一个ostream对象赋值给_IO_ostream_withassign对象
        return *this;
    }

    _IO_ostream_withassign& operator=(_IO_ostream_withassign& rhs) {
        // 实现赋值操作
        return operator=(static_cast<ostream&>(rhs));
    }
};

extern _IO_ostream_withassign cout;

6.moveable元素对于vector速度效能的影响

  • 深拷贝:创建一个新对象,并复制所有的原始对象的数据,包括指向动态分配内存的指针。深拷贝确保新对象和原始对象独立,修改一个不会影响另一个。

    // 拷贝构造函数
    MyString(const MyString& other) {
        len = other.len;
        data = new char[len + 1];  // 为新对象开辟内存
        strcpy(data, other.data);  // 将原对象的数据复制到新对象
    }
    
    // 拷贝赋值运算符
    MyString& operator=(const MyString& str) {
        if (this != &str) {
            if (_data) delete[] _data;
            _len = str._len;
            _init_data(str._data);  // 复制数据
        }
        return *this;
    }
    
  • 浅拷贝:只复制对象的成员变量的值,包括指针的值。这意味着复制后的对象和原始对象共享同一块动态分配的内存。这可能会造成一个对象销毁时释放了共享的内存时,导致另一个对象访问无效内存。

    MyString(const Mystring& other) {
    		len = other.len;
    		data = other.data;
    }
    
  • 移动语义(Move Semantics):通过转移指针操作将资源从一个对象转移到另一个对象,避免深拷贝。通过移动构造函数和移动赋值运算符实现。

    #include <utility>
    class  MyString {
    public:
    		// 移动构造函数
    		// data = other.data; 新对象直接使用原对象的指针。
    		MyString(MyString&& other) noexcept: data(other.data), len(other.len) {
    				other.data = nullptr; // 将原对象的指针设置为nullptr,防止原对象析构时释放内存
            other.len = 0;
        }
        
        // 移动赋值运算符
        MyString& operator=(MyString&& other) noexcept {
            if (this == &other) return *this;
            delete[] data;
            data = other.data;
            len = other.len;
            other.data = nullptr;
            other.len = 0;
            return *this;
        }
        
        // 析构函数
        ~MyString() {
        ++Dtor;
        if (_data) delete[] _data;
    		}
    		
        // 禁用拷贝构造函数和拷贝赋值运算符
        MyString(const MyString& other) = delete;
        MyString& operator=(const MyString& other) = delete;
    
  • 具体示例

    int main() {
    		Mystring str1("Hello, world!");
    		// 调用拷贝构造函数
    		Mystring str2 = str1;
    		cout << str2 << endl;  // "Hello, world!"
    		
    		// 调用移动构造函数
    		Mystring str3 = move(str1);
    		cout << str3 << endl;  // "Hello, world!"
    		cout << str1 << endl;  // 空,因为str1的资源已经转移
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1697930.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Topk问题以及二叉树的三种层序遍历和基本操作

一、Topk问题 1、问题描述 TOP-K问题&#xff1a;即求数据结合中前K个最大的元素或者最小的元素&#xff0c;一般情况下数据量都比较大。 比如&#xff1a;专业前10名、世界500强、富豪榜、游戏中前100的活跃玩家等。 2、思路 对于Top-K问题&#xff0c;能想到的最简单直接的…

C++数据结构——哈希桶HashBucket

目录 一、前言 1.1 闭散列 1.2 开散列 1.3 string 与 非 string 二、哈希桶的构成 2.1 哈希桶的节点 2.2 哈希桶类 三、 Insert 函数 3.1 无需扩容时 3.2 扩容 复用 Insert&#xff1a; 逐个插入&#xff1a; 优缺点比对&#xff1a; 第一种写法优点 第一种写法…

0成本的副业兼职,虚拟资源项目,1单利润49,操作简单变现快

最新刷某音时&#xff0c;我意外地发现了一位同行&#xff0c;他正在出售一份某音运营和直播的资料。然而&#xff0c;他销售这份资料的方式非常独特。他将这些所谓的某音运营资料全部打印出来。 周周近财&#xff1a;让网络小白少花冤枉钱&#xff0c;赚取第一桶金 每个视频的…

GESP等级大纲

CCF编程能力等级认证概述 CCF编程能力等级认证&#xff08;GESP&#xff09;为青少年计算机和编程学习者提供学业能力验证的规则和平台。GESP覆盖中小学阶段&#xff0c;符合年龄条件的青少年均可参加认证。C & Python编程测试划分为一至八级&#xff0c;通过设定不同等级…

CAD二次开发(2)-将直线对象添加到CAD图形文件

1. 准备工作 创建一个类库项目&#xff0c;如下&#xff1a; 2. 分析Line对象 Line类的初始化方法和参数 using Autodesk.AutoCAD.DatabaseServices; Line line new Line();Line 继承Curve 继承Entity 继承DBObject 继承Drawable 继承RXObject 初始化方法有两个&#xf…

探索微软Edge开发者工具:优化前端开发的艺术与科学

探索微软Edge开发者工具&#xff1a;优化前端开发的艺术与科学 引言&#xff1a;Edge开发者工具概览一、基础操作&#xff1a;步入DevTools的大门1.1 启动与界面布局1.2 快速导航与定制 二、元素审查与样式调整2.1 精准元素选取2.2 实时CSS编辑2.3 自动完成与内联文档 三、Java…

代码随想录|Day56|动态规划 part16|● 583. 两个字符串的删除操作 ● 72. 编辑距离

583. 两个字符串的删除操作 class Solution: def minDistance(self, word1: str, word2: str) -> int: dp [[0] * (len(word2) 1) for _ in range(len(word1) 1)] for i in range(len(word1) 1): dp[i][0] i for j in range(len(word2) 1): dp[0][j] j for i in rang…

js 生成二维码

第一种&#xff0c;需要先下载包 npm install qrcode <template><div><h2>二维码</h2><img :src"qrCodeImage" alt""></div> </template><script> import QRCode from qrcode export default {dat…

【OpenGL实践12】关于缓存区Framebuffer的运用

文章目录 一、说明二、帧缓冲区三、创建新的帧缓冲区四、附属装饰4.1 纹理图像4.2 渲染缓冲区对象图像 五、使用帧缓冲区5.1 后期处理5.2 更改代码 六、后期处理效果6.1 色彩处理6.2 模糊6.3 Sobel算子 七、结论练习 一、说明 关于FrameBuffer的使用&#xff0c;是OpenGL的高级…

spark实战:实现分区内求最大值,分区间求和以及获取日志文件固定日期的请求路径

spark实战&#xff1a;实现分区内求最大值&#xff0c;分区间求和以及获取日志文件固定日期的请求路径 Apache Spark是一个广泛使用的开源大数据处理框架&#xff0c;以其快速、易用和灵活的特点而受到开发者的青睐。在本文中&#xff0c;我们将通过两个具体的编程任务来展示S…

spiderfoot一键扫描IP信息(KALI工具系列九)

目录 1、KALI LINUX简介 2、spiderfoot工具简介 3、在KALI中使用spiderfoot 3.1 目标主机IP&#xff08;win&#xff09; 3.2 KALI的IP 4、命令示例 4.1 web访问 4.2 扫描并进行DNS解析 4.3 全面扫描 5、总结 1、KALI LINUX简介 Kali Linux 是一个功能强大、多才多…

Spring Boot集成testcontainers快速入门Demo

1.什么是testcontainers&#xff1f; Testcontainers 是一个用于创建临时 Docker 容器进行单元测试的 Java 库。当我们想要避免使用实际服务器进行测试时&#xff0c;它非常有用。&#xff0c;官网介绍称支持50多种组件。​ 应用场景 数据访问层集成测试&#xff1a; 使用My…

掌握ASPICE标准:汽车软件测试工程师的专业发展路径

掌握ASPICE标准&#xff1a;汽车软件测试工程师的专业发展路径 文&#xff1a;领测老贺 随着新能源汽车在中国的蓬勃发展&#xff0c;智能驾驶技术的兴起&#xff0c;汽车测试工程师的角色变得愈发关键。这一变革带来了前所未有的挑战和机遇&#xff0c;要求测试工程师不仅要具…

Matlab中函数或变量 ‘eeglab‘ 无法识别

EEGLAB 没有安装或添加到 MATLAB 路径中&#xff1a; 确保已经安装了 EEGLAB&#xff0c;并且将其添加到 MATLAB 的路径中。您可以通过在 MATLAB 命令窗口中运行 which eeglab 来检查是否能够找到 EEGLAB。 EEGLAB 函数路径设置错误&#xff1a; 如果已经安装了 EEGLAB&#x…

Mac | Mac 移动硬盘无法分区问题

现象问题 电脑配置&#xff1a;MacBook Pro M1&#xff0c;系统 Sonoma Mac 系统新升级了 Sonoma&#xff0c;结果出现各种问题。外接屏幕居然不能旋转 90 &#xff0c;查了一下是Sonoma系统导致的&#xff0c;以及莫名发热的问题。想着要么回退一下系统算了&#xff0c;于是网…

Sql Sever删除数据库时提示数据库正在被使用,解决办法

报错解释&#xff1a; 当您尝试删除SQL Server中的某个对象&#xff08;如数据库、表等&#xff09;时&#xff0c;如果有程序或进程正在使用该对象&#xff0c;您可能会收到一个错误信息&#xff0c;提示该对象正被使用。这通常是因为还有一个或多个数据库连接仍然保持着对该…

使用libtorch加载YOLOv8生成的torchscript文件进行目标检测

在网上下载了60多幅包含西瓜和冬瓜的图像组成melon数据集&#xff0c;使用 LabelMe 工具进行标注&#xff0c;然后使用 labelme2yolov8 脚本将json文件转换成YOLOv8支持的.txt文件&#xff0c;并自动生成YOLOv8支持的目录结构&#xff0c;包括melon.yaml文件&#xff0c;其内容…

网络通信(二)

UDP通信 特点&#xff1a;无连不是先接、不可靠通信 不事先建立连接&#xff1b;发送端每次把要发送的数据&#xff08;限制在64KB内&#xff09;、接收端IP、等信息封装成一个数据包&#xff0c;发出去就不管了 java提供了一个java.net.DatagramSocket类来实现UDP通信 Dat…

20.SkyWalking

一.简介 SkyWalking用于应用性能监控、分布式链路跟踪、诊断&#xff1a; 参考连接如下&#xff1a; https://github.com/apache/skywalking https://skywalking.apache.org/docs/ 二.示例 通过官网连接进入下载页面&#xff1a;https://archive.apache.org/dist/skywalkin…

普通人转行程序员,最大的困难是找不到就业方向

来百度APP畅享高清图片 大家好&#xff0c;这里是程序员晚枫&#xff0c;小破站也叫这个名。 我自己是法学院毕业后&#xff0c;通过2年的努力才转行程序员成功的。[吃瓜R] 我发现对于一个外行来说&#xff0c;找不到一个适合自己的方向&#xff0c;光靠努力在一个新的行业里…