新版的hash_map都是unordered_map了,这里只说unordered_map和map.
运行效率方面:unordered_map最高,而map效率较低但 提供了稳定效率和有序的序列。
占用内存方面:map内存占用略低,unordered_map内存占用略高,而且是线性成比例的。
需要无序容器,快速查找删除,不担心略高的内存时用unordered_map;有序容器稳定查找删除效率,内存很在意时候用map。
map的内部实现是二叉平衡树(红黑树);hash_map内部是一个hash_table一般是由一个大vector,vector元素节点可挂接链表来解决冲突,来实现.
#include
#include
#include
#include
#include
#pragma comment(lib,"psapi.lib")
using namespace std;
using namespace stdext;
void showMemoryInfo(void)
{
HANDLE handle = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
cout << "Memory Use:" << pmc.WorkingSetSize/1024.0f << "KB/" << pmc.PeakWorkingSetSize/1024.0f << "KB, Virtual Memory Use:" << pmc.PagefileUsage/1024.0f << "KB/" << pmc.PeakPagefileUsage/1024.0f << "KB" << endl;
}
//define the class
/*-------------------------------------------*/
/*函数类
*作为hash_map的hash函数
*string没有默认的hash函数
*/
class str_hash {
public:
size_t operator()(const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0; i < str.size(); i++)
__h = 5 * __h + str[i];
return size_t(__h);
}
};
/*-------------------------------------------*/
/*函数类
*作为hash_map的比较函数 )
*(查找的时候不同的key往往可能对用到相同的hash值
*/
class str_compare
{
public:
bool operator()(const string& str1, const string& str2)const
{
return str1 == str2;
}
};
struct CharLess : public binary_function
{
public:
result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const
{
return(_Left.compare(_Right) < 0 ? true : false);
}
};
int main()
{
cout << "Test HashMap(unorder map) Memory Use Start..."<< endl;
// VC下自定义类型
unordered_map > CharHash;
for (int i = 0; i < 10000000; i++)
{
string key = to_string(i);
CharHash[key] = i;
}
cout << "Test HashMap(unorder map) Memory Use End." << endl;
showMemoryInfo();
while (1);
return 0;
}
#include
#include
#include "stdafx.h"
// 存放过程:key->hash函数->hash值对桶数求模得到桶号(桶有值则解决冲突),存放key和value在桶内
// 取回过程:key->hash函数->hash值对桶数求模得到桶号(桶有值则解决冲突),比较桶内的key是否相等,
// 若不相等则返回空迭代器,否则返回迭代器。
// 1.hash_map为下面类型的key定义了hash寻址函数(用于从key到hash值)和哈希比较函数(用于解决冲突)。
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
//struct hash
// 内建的类型直接 hash_map mymap;像普通map一样使用即可。
// 2.自定义hash函数和比较函数
//在声明自己的哈希函数时要注意以下几点:
//使用struct,然后重载operator().
//返回是size_t
//参数是你要hash的key的类型。
//函数是const类型的。
// 定义自己的比较函数:
//使用struct,然后重载operator().
//返回是bool
//参数是你要hash的key的类型的两个常量参数,用于比较。
//函数是const类型的。
// 自定义hash函数和比较函数的使用:
// hash_map hmap;
// 3.hash_map使用的常用函数
//hash_map的函数和map的函数差不多。具体函数的参数和解释,请参看:STL 编程手册:Hash_map,这里主要介绍几个常用函数。
//
//hash_map(size_type n) 如果讲究效率,这个参数是必须要设置的。n 主要用来设置hash_map 容器中hash桶的个数。
//桶个数越多,hash函数发生冲突的概率就越小,重新申请内存的概率就越小。n越大,效率越高,但是内存消耗也越大。
//
//const_iterator find(const key_type& k) const. 用查找,输入为键值,返回为迭代器。
//
//data_type& operator[](const key_type& k) . 这是我最常用的一个函数。因为其特别方便,可像使用数组一样使用。
//不过需要注意的是,当你使用[key ]操作符时,如果容器中没有key元素,这就相当于自动增加了一个key元素。
//因此当你只是想知道容器中是否有key元素时,你可以使用find。如果你希望插入该元素时,你可以直接使用[]操作符。
//
//insert 函数。在容器中不包含key值时,insert函数和[]操作符的功能差不多。但是当容器中元素越来越多,
//每个桶中的元素会增加,为了保证效率,hash_map会自动申请更大的内存,以生成更多的桶。因此在insert以后,
//以前的iterator有可能是不可用的。
//
//erase 函数。在insert的过程中,当每个桶的元素太多时,hash_map可能会自动扩充容器的内存。
//但在sgi stl中是erase并不自动回收内存。因此你调用erase后,其他元素的iterator还是可用的。
#include
#include
#include
using namespace std;
using namespace stdext;
//define the class
/*-------------------------------------------*/
/*函数类
*作为hash_map的hash函数
*string没有默认的hash函数
*/
class str_hash{
public:
size_t operator()(const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return size_t(__h);
}
};
/*-------------------------------------------*/
/*函数类
*作为hash_map的比较函数 )
*(查找的时候不同的key往往可能对用到相同的hash值
*/
class str_compare
{
public:
bool operator()(const string& str1,const string& str2)const
{return str1==str2;}
};
struct CharLess : public binary_function
{
public:
result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const
{
return(strcmp(_Left, _Right) < 0 ? true : false);
}
};
int main()
{
// 内建类型
hash_map myHashMap;
myHashMap[0] = "JesseCen";
myHashMap[1] = "OZZ";
hash_map::iterator itrHash = myHashMap.find(0);
if(itrHash != myHashMap.end())
{
cout<<"My Name is:"<second.c_str()< > CharHash;
CharHash["a"] = 123;
CharHash["b"] = 456;
hash_map >::iterator itrChar = CharHash.find("b");
if( itrChar != CharHash.end())
{
cout<<"The find number is:"<< itrChar->second<
参考文章: