undered_map Part.1

unordered_map是C++11引入的STL容器,基于哈希表,提供常数级查找效率,对比map在数据密集场景下优势明显。文章通过一个例子展示了unordered_map的正确使用,并指出在数据量极大时,二叉树查找可能更快。
摘要由CSDN通过智能技术生成

unordered_map是什么

unordered_map是boost库中的一种非标准STL,在C++11中,将其加入了体系,我们知道,map是基于二叉树查找的容器,是nlogn的时间复杂度,而unordered_map的时间复杂度是一个常数级,和这个容器(container)存储数据的多少是没有关系的,例如说在100个数据中查找,map可能需要5-6次,而unordered_map只需要1次就可以完成,可见,效率是很高的。
理想状况下,unordered_map可以提到map容器,hash查找速度要比二叉树要快,但是并非hash查找就可以完全替代二叉树查找,有人做过测试,当数据超过一千万次后,二叉树查找要比hash快,我个人认为,对于数据密集的例子,hash查找的作用更大,因为数据间隙小,数据密度大,而对于一些数据并不密集的例子,使用hash查找就是典型的用空间换时间了,对于空间有限的情况下并不合适。

一个例子

下面是一个例子,unordered_map的正确用法。
详解已经放在代码段的主函数注释中,这里我是使用自定义类型的Key-Value对,和map类似,我们需要给自定义的Key显示的设置Hash值的算法。

//unordered_map

//unordered_map/multimap
//C++11's new Container(vector),It previous from boost library

template<typename Type>
class Key_Position
{
public:
	Key_Position(Type _X, Type _Y) :x(_X), y(_Y) {}
	Type Get_X() const { return x; }
	Type Get_Y() const { return y; }
private:
	Type x = Type();
	Type y = Type();
};

//Best Hash in Math //Boost
template<class T>inline void hash_combine(std::size_t& seed, const T& v) {
	std::hash<T>hasher;
	seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

//class template specialization
namespace std {
	template<typename Type>struct hash<Key_Position<Type>> {
		Type operator()(const Key_Position<Type>& report)const {
			//return report.Get_X() * report.Get_Y();//custom hash
			auto Key = hash<int>(report.Get_X);
			hash_combine(Key, report.Get_Y);
			return  Key;//STD's hash
		}
	};
}

class Player
{
public:
	Player(std::string nName, double aAge) :Age(aAge), Name{ std::move(nName) }{}
	double Get_Age(){return Age;}
	const std::string& Get_Name() const{return Name;}
	void Updata_Name(std::string& NewName){this->Name = NewName;}
	void Updata_Age(double New_Age){this->Age = New_Age;}
	void ShowPlayer(){std::cout << "The Player called: " << Name << " Age: " << Age << std::endl;}
	bool operator==(const Player& rhs) const{return rhs.Age == Age && rhs.Name == Name;}
private:
	std::string Name;
	double Age;
};

int main()
{
	//std::unordered_map<int, std::string> mymap;
	using Group?
	//using Group = std::unordered_map<int, std::string>;//Why?
	//Group group;//Group is a Class,group is a object?
	/*
	template<typename Key,typenameT,
			typename Hash = hash<Key>,
			typename EqPred = equal_to<Key>,
			typename Allocator = allocator<pair<const Key,T> > >

	Key: It appear(arise) only once in The Container,if it appear more than once,IDE well report error.
	T:Value name,the same as std::map's value,we can definition many of Data Types
	Hash:in usually,The algorithm is default,unorder_map base in hash_table,so that, Hash is use for count the Hash_Value to save the hash_table
	EqPred:in usually,The algorithm is default,compare with Right_Key and Left_Key,if the same,the function return true,IDE well report error.
	Allocator:in usually,The algorithm is default,it provide storge mode.

	Attention:Hash,EqPred,Allocator;these are default setting,if we need custom "Key" and "T",we should achive the custom Hash Function
	for system to understand the value'mean and how to run them.
	*/

	std::unordered_map<Key_Position<int>, Player> MyMap;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值