c++用哈希表存储链表的理解

c++用哈希表存储链表的理解,实现过程的文字描述

img

如何理解上述图片的运行过程

先了解unordered_map的原理

m.find(2)  //查找key为2的键值对是否存在 ,若没找到则返回m.end()
if(m.find(2)!=m.end()) //判断找到了key为2的键值对


  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7399879
  • 你也可以参考下这篇文章:数据结构:C++ 循环双链表根据查找频度进行位置重组,使频繁访问的结点总是靠近表头
  • 除此之外, 这篇博客: C++指针引用和指向指针的指针传输结构体中的 C++指针引用和指向指针的指针传输结构体 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include <iostream>
    using namespace std;
    
    struct teacher {
    	int id;
    	char name[64];	
    };
    
    int tea(struct teacher* &a) {//指针引用,a是c的别名
    	
    	a = (struct teacher*)malloc(sizeof(struct teacher));//a = 开辟的内存,相当于c开辟内存,在写入数据
    	if (a == NULL)
    	{
    		return -1;
    	}
    	a->id = 100;
    	strcpy_s(a->name, "礼仪");
    	return 0;
    }
    
    int tea1(struct teacher **a) {//指向指针的指针指向a(函数的实参)指针的取地址
    	struct teacher *t = NULL;//a=实参的取地址,&a是a(指向指针的指针)的取地址,*a是a指向内存的内容
    	if (a==NULL)             
    	{
    		return -1;
    	}
    	
    	t = (struct teacher*)malloc(sizeof(struct teacher));//理解为某指针指向某地址,指针t = 某指针
    	       
    	//cout << t << endl;//t是一个指针,指向在内存中开辟的一个地址,t=某地址
    	//cout << &t << endl;//&t是t自己的取地址
    	//cout << a << endl;//
    	//cout << *a << endl;
    	//cout << &a << endl;
    	//cout << &(*a) << endl;
    	t->id = 100;	
    	strcpy_s(t->name, "哈哈");
    	*a = t;
    	return 0;
    }
    
    void rea1(struct teacher **a) {
    	if (a==NULL)
    	{
    		return;
    	}
    	struct teacher* t = *a;
    	if (t!=NULL)
    	{
    		free(t);//开辟内存,要释放
    		*a = NULL;		
    	}
    }
    
    void rea(struct teacher *&a) {
    	if (a==NULL)
    	{
    		return;
    	}
    	else
    	{
    		free(a); //开辟内存,要释放
    		a = NULL;
    	}
    }
    
    void main() {
    	struct teacher *c = NULL;//定义一个指针作传输体
    
    	tea1(&c);
    	cout << "id=" <<c->id<<"名字:"<< c->name << endl;//极有可能是*(c->id)
    	rea1(&c);
    	tea(c);
    	cout<< "id=" <<c->id<<"名字:"<< c->name << endl;
    	rea(c);
    }
    
    
    

    效果图:
    在这里插入图片描述

  • 您还可以看一下 夏曹俊老师的C++微服务架构及安全云盘项目实训课程中的 完成了客户端的文件上传指令处理小节, 巩固相关知识点