数据结构-哈希与映射

本文深入探讨了C++中的不同映射类型,包括二叉树映射(map)、线性映射(LinearMap)、哈希映射(HashMap)以及C++标准库中的哈希映射(hash_map),对比了它们的实现方式、性能特点和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

二叉树映射map

#include<iostream>
#include<map>  //映射(也叫字典),二叉树映射,不是哈希映射
using namespace std;
int main()
{
	cout << "二叉树(红黑树)" << endl;
	map<string, int> m;
	m["bill"]= 67;
	m["toney"] = 98;
	m["Mary"] = 100;

	cout << m["Mary"] << endl;
	system("pause");
	return 0;
}

数组优点 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	//数组的优点
	//对下标(索引)进行非常快的地址运算
	//取arr[0]  arr[23232]  速度一样
	int arr[100000];
	int length = sizeof(arr) / sizeof(int);
	cout << length << endl;
	for (int i = 0; i < length; i++)
	{
		arr[i] = i%100;
	}
	cout << arr[8] << endl;
	cout << arr[18] << endl;
	cout << arr[98756] << endl;

	system("pause");
	return 0;
}

线性映射 

LinearMap.h

#ifndef LINEARMAP_H
#define LINEARMAP_H
#include<vector>
template<class Key,class Value>
class LinearMap  //线性映射-没用的东西
{
public:
	LinearMap(int size = 101) :arr(size)
	{
		currentSize = 0;
	}
	void Put(const Key &k, const Value &v)
	{
		arr[currentSize] = DataEntry(k, v);
		++currentSize;
	}
	Value Get(const Key & k)
	{
		//线性查找
		for (size_t i = 0; i < currentSize; ++i)
		{
			if (arr[i].key == k)
			{
				return arr[i].value;
			}
		}
		return Value();
	}
private:
	struct DataEntry {
		Key key;
		Value value;
		DataEntry(const Key & k = Key(),
			const Value & v = Value()) : key(k), value(v) {}
	};
	std::vector<DataEntry> arr;
	int currentSize;
};

#endif

linearMap.cpp

#include<iostream>
#include<string>
#include"LinearMap.h"
using namespace std;
int main()
{
	//没用的,仅学习的线性映射
	LinearMap<string, int> lm;
	lm.Put("bill", 99);
	lm.Put("Tom", 88);
	cout << "Lin  earMap:" << lm.Get("Tom") << endl;
	system("pause");
	return 0;
}

运行结果:

哈希映射

HashMap.h

#ifndef LINEARMAP_H
#define LINEARMAP_H
#include<vector>
template<class Key, class Value>
class HashMap  //哈希映射 最快
{
public:
	HashMap(int size = 101) :arr(size)
	{
		currentSize = 0;
	}
	void Put(const Key &k, const Value &v)
	{
		int pos = myHash(k);
		arr[pos] = DataEntry(k, v);
		++currentSize;
	}
	Value Get(const Key & k)
	{
		int pos = myHash(k);
		if (arr[pos].key == k)
			return arr[pos].value;
		else
			return Value();
	}

	unsigned hash(const Key & k) const
	{
		unsigned int hashVal = 0;
		const char* keyp = reinterpret_cast<const char*>(&k);  //转型
		for (size_t i = 0; i < sizeof(Key); i++)
		{
			hashVal = 37 * hashVal + keyp[i];
		}
		return hashVal;
	}

	int myHash(const Key & k) const
	{
		unsigned hashVal = hash(k);
		hashVal %= arr.size();
		return hashVal;
	}

private:
	struct DataEntry {
		Key key;
		Value value;
		DataEntry(const Key & k = Key(),
			const Value & v = Value()) : key(k), value(v) {}
	};
	std::vector<DataEntry> arr;
	int currentSize;
};

#endif

HashMap.cpp

#include<iostream>
#include<string>
#include"HashMap.h"
using namespace std;
int main()
{
	
	HashMap<string, int> hm;
	/*cout << hm.hash("Bill") << endl;
	cout << hm.myHash("Bill") << endl;*/
	hm.Put("Bill", 33);
	hm.Put("Tom", 98);
	cout << hm.Get("Bill") << endl;
	//cout << hm.Get("Tom") << endl;  取两个就会错误
	system("pause");
	return 0;
}

运行结果:

c++自带的哈希映射(STL)

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include<iostream>
#include<string>
#include<hash_map>
using namespace std;
//使用C++封装好的hash_map
//又要排序又要映射时就用tree_map(二叉树映射)
int main()
{
	hash_map<string, int> hm;
	hm["Bill"] = 12;
	hm["Tom"] = 22;
	hm["Mary"] = 34;
	cout << hm["Mary"] << endl;
	cout << hm["Tom"] << endl;
	system("pause");
	return 0;
} 

运行结果:

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chde2Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值