一:无序容器简介

Unordered Containers也是一种关联式容器。其中元素是分散,没有定性的排列(不是图中那样松散)。其中元素可能在某一次操作后改变原来的位置。

哈希表的链地址法,更能表现其内部实现结构。其中哈希表中表长可以改变,其实现用分配器实现,
为了防止链表过程,效率减低,设置一个值,当链表长度过长时(大于等于表长),打散哈希表,重新设置表长,分配位置。

二:性能测试

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <cstring> #if _MSC_VER
#define snprintf _snprintf
#endif using namespace std; long get_a_target_long()
{
/******变量声明********/
long target = ;
/**********************/ cout << "targer (0~" << RAND_MAX << "):";
cin >> target;
return target;
} string get_a_target_string()
{
/******变量声明********/
long target = ;
char buf[];
/**********************/ cout << "targer (0~" << RAND_MAX << "):";
cin >> target; snprintf(buf, , "%d", target);
return string(buf);
} //与后面的比较函数中回调参数对应
int compareLongs(const void* l1, const void* l2)
{
return (*(long*)l1 - *(long*)l2);
} int compareStrings(const void* s1, const void* s2)
{
if (*(string*)s1 > *(string*)s2)
return ;
if (*(string*)s1 < *(string*)s2)
return -;
return ;
}

公共函数

(一)unordered_multiset测试

#include <unordered_set>
//测试unordered_multiset-->元素可以重复
namespace jj12
{
void test_unordered_multiset(long& us_size)
{
cout << "\ntest_unordered_multiset()*******" << endl; /******变量声明:数组初始********/
char buf[]; /******变量声明:unordered_multiset初始********/
unordered_multiset<string> ums; /******变量声明:记录时间********/
clock_t timeStart = clock(); //开始时间
for (long i = ; i < us_size; i++)
{
try
{
snprintf(buf, , "%d", rand());
ums.insert(string(buf));
}
catch (exception& e)
{
cout << e.what() << endl;
cout << "Max_size:" << i << endl;
abort(); //终止
}
} cout << "inti unordered_multiset use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时
cout << "unordered_multiset.size:" << ums.size() << endl; //获取unordered_multiset大小
cout << "unordered_multiset.max_size:" << ums.max_size() << endl; //获取unordered_multiset允许最大长度
cout << "unordered_multiset.bucket_count:" << ums.bucket_count() << endl; //获取篮子数--表长
cout << "unordered_multiset.load_factor:" << ums.load_factor() << endl; //获取加载因子
cout << "unordered_multiset.max_load_factoe:" << ums.max_load_factor() << endl; //获取最大加载因子--1
cout << "unordered_multiset.max_bucket_count:" << ums.max_bucket_count() << endl; //获取存在最大篮子数--表长 //打印前20个篮子
for (int i = ; i < ; i++)
cout << "Key #" << i << " has " <<
ums.bucket_size(i) //该篮子中有几个元素
<< " elements" << endl; /******变量声明:获取我们要查询的数********/
string target = get_a_target_string(); //使用::find方法进行查找
timeStart = clock(); auto pI = find(ums.begin(), ums.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl;
if (pI != ums.end())
cout << "found:" << *pI << endl;
else
cout << "not found!" << endl; //使用unordered_multiset.find查找
timeStart = clock(); pI = ums.find(target); //比::find块得多,直接定位查询, cout << "unordered_multiset.find(),milli-seconds:" << clock() - timeStart << endl;
if (pI != ums.end())
cout << "found:" << *pI << endl;
else
cout << "not found!" << endl; }
}

(二)unordered_multimap测试

#include <unordered_map>
//测试unordered_multimap-->元素可以重复
namespace jj13
{
void test_unordered_multimap(long& mm_size)
{
cout << "\ntest_unordered_multimap()*******" << endl; /******变量声明:数组初始********/
char buf[]; /******变量声明:unordered_multimap初始********/
unordered_multimap<long, string> umm; /******变量声明:记录时间********/
clock_t timeStart = clock(); //开始时间
for (long i = ; i < mm_size; i++)
{
try
{
snprintf(buf, , "%d", rand());
umm.insert(pair<long, string>(i, string(buf)));
}
catch (exception& e)
{
cout << e.what() << endl;
cout << "Max_size:" << i << endl;
abort(); //终止
}
} cout << "inti unordered_multimap use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时
cout << "unordered_multimap.size:" << umm.size() << endl; //获取unordered_multimap大小
cout << "unordered_multimap.max_size:" << umm.max_size() << endl; //获取unordered_multimap所允许最大
cout << "unordered_multimap.bucket_count:" << umm.bucket_count() << endl; //获取篮子数--表长
cout << "unordered_multimap.load_factor:" << umm.load_factor() << endl; //获取加载因子
cout << "unordered_multimap.max_load_factoe:" << umm.max_load_factor() << endl; //获取最大加载因子--1
cout << "unordered_multimap.max_bucket_count:" << umm.max_bucket_count() << endl; //获取存在最大篮子数--表长 //打印前20个篮子
for (int i = ; i < ; i++)
cout << "Key #" << i << " has " <<
umm.bucket_size(i) //该篮子中有几个元素
<< " elements" << endl; /******变量声明:获取我们要查询的数********/
long target = get_a_target_long(); //根据key查找 //unordered_multimap没有全局::find方法可用,::find找值,multimap找键,两者不同,不可以混用 //使用unordered_multimap.find查找
timeStart = clock(); auto pI = umm.find(target); cout << "unordered_multimap.find(),milli-seconds:" << clock() - timeStart << endl;
if (pI != umm.end())
cout << "found:" << (*pI).first << ":" << (*pI).second << endl;
else
cout << "not found!" << endl; }
}

(三)unordered_set测试

#include <unordered_set>
//测试unordered_set-->元素不可以重复
namespace jj14
{
void test_unordered_set(long& us_size)
{
cout << "\ntest_unordered_set()*******" << endl; /******变量声明:数组初始********/
char buf[]; /******变量声明:unordered_set初始********/
unordered_set<string> us; /******变量声明:记录时间********/
clock_t timeStart = clock(); //开始时间
for (long i = ; i < us_size; i++)
{
try
{
snprintf(buf, , "%d", rand());
us.insert(string(buf));
}
catch (exception& e)
{
cout << e.what() << endl;
cout << "Max_size:" << i << endl;
abort(); //终止
}
} cout << "inti unordered_multiset use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时
cout << "unordered_set.size:" << us.size() << endl; //获取unordered_set大小
cout << "unordered_set.max_size:" << us.max_size() << endl; //获取unordered_set允许最大
cout << "unordered_set.bucket_count:" << us.bucket_count() << endl; //获取篮子数--表长
cout << "unordered_set.load_factor:" << us.load_factor() << endl; //获取加载因子
cout << "unordered_set.max_load_factoe:" << us.max_load_factor() << endl; //获取最大加载因子--1
cout << "unordered_set.max_bucket_count:" << us.max_bucket_count() << endl; //获取存在最大篮子数--表长 //打印前20个篮子
for (int i = ; i < ; i++)
cout << "Key #" << i << " has " <<
us.bucket_size(i) //该篮子中有几个元素
<< " elements" << endl; /******变量声明:获取我们要查询的数********/
string target = get_a_target_string(); //使用::find方法进行查找
timeStart = clock(); auto pI = find(us.begin(), us.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl;
if (pI != us.end())
cout << "found:" << *pI << endl;
else
cout << "not found!" << endl; //使用unordered_set.find查找
timeStart = clock(); pI = us.find(target); //比::find块得多,直接定位查询, cout << "unordered_set.find(),milli-seconds:" << clock() - timeStart << endl;
if (pI != us.end())
cout << "found:" << *pI << endl;
else
cout << "not found!" << endl; }
}

(四)unordered_map测试

#include <unordered_map>
//测试unordered_map-->元素不可以重复
namespace jj15
{
void test_unordered_map(long& um_size)
{
cout << "\ntest_unordered_map()*******" << endl; /******变量声明:数组初始********/
char buf[]; /******变量声明:unordered_multimap初始********/
unordered_map<long, string> um; /******变量声明:记录时间********/
clock_t timeStart = clock(); //开始时间
for (long i = ; i < um_size; i++)
{
try
{
snprintf(buf, , "%d", rand());
um.insert(pair<long, string>(i, string(buf)));
}
catch (exception& e)
{
cout << e.what() << endl;
cout << "Max_size:" << i << endl;
abort(); //终止
}
} cout << "inti unordered_map use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时
cout << "unordered_map.size:" << um.size() << endl; //获取unordered_map大小
cout << "unordered_map.max_size:" << um.max_size() << endl; //获取unordered_map允许最大
cout << "unordered_map.bucket_count:" << um.bucket_count() << endl; //获取篮子数--表长
cout << "unordered_map.load_factor:" << um.load_factor() << endl; //获取加载因子
cout << "unordered_map.max_load_factoe:" << um.max_load_factor() << endl; //获取最大加载因子--1
cout << "unordered_map.max_bucket_count:" << um.max_bucket_count() << endl; //获取存在最大篮子数--表长 //打印前20个篮子
for (int i = ; i < ; i++)
cout << "Key #" << i << " has " <<
um.bucket_size(i) //该篮子中有几个元素
<< " elements" << endl; /******变量声明:获取我们要查询的数********/
long target = get_a_target_long(); //根据key查找 //unordered_map没有全局::find方法可用,::find找值,unordered_map找键,两者不同,不可以混用 //使用unordered_map.find查找
timeStart = clock(); auto pI = um.find(target); cout << "unordered_map.find(),milli-seconds:" << clock() - timeStart << endl;
if (pI != um.end())
cout << "found:" << (*pI).first << ":" << (*pI).second << endl;
else
cout << "not found!" << endl; }
}

10--STL无序容器(Unordered Containers)的更多相关文章

  1. C++ 标准模板库(STL)——容器(Containers)的用法及理解

    C++ 标准模板库(STL)中定义了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量(vector).队列(queue).栈(stack).set.map等.这次主要 ...

  2. STL学习笔记— —无序容器(Unordered Container)

    简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...

  3. STL基础--容器

    容器种类 序列容器(数组,链表) Vector, deque, list, forward list, array 关联容器(二叉树),总是有序的 set, multiset根据值排序,元素值不能修改 ...

  4. STL之容器基本操作

    容器类 STL Container Header Applications vector <vector> 直接访问任意元素,快速插入.删除尾部元素 deque <deque> ...

  5. [C++ STL] 各容器简单介绍

    什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...

  6. STL List容器

    转载http://www.cnblogs.com/fangyukuan/archive/2010/09/21/1832364.html 各个容器有很多的相似性.先学好一个,其它的就好办了.先从基础开始 ...

  7. c++复习:STL之容器

    1 STL的string 1 String概念 string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都可以用来表示字 ...

  8. C++ STL bitset 容器详解

    C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...

  9. 详解C++ STL set 容器

    详解C++ STL set 容器 本篇随笔简单介绍一下\(C++STL\)中\(set\)容器的使用方法及常见使用技巧. set容器的概念和性质 \(set\)在英文中的意义是:集合.\(set\)容 ...

随机推荐

  1. Ajax 的简介与使用

    一.什么是Ajax Ajax 的全称是 Asynchronous JavaScript and XML(即异步的 JavaScript 和 XML),是一种在无需重新加载整个网页的情况下,能够更新部分 ...

  2. shell 文本替换 ---出现--- sed:-e 表达式 #1,字符 8:“s”的未知选项

    需要替换的行为: monitor.url=http://192.168.25.100:8443/rest 查询资料得知,报错是因为替换的字符串包含有分隔符/ 所以这行改一下分隔符就可以解决问题了  ( ...

  3. linux网络编程之socket编程(十二)

    今天继续学习socket编程,期待的APEC会议终于在京召开了,听说昨晚鸟巢那灯火通明,遍地礼花,有点08年奥运会的架势,有种冲动想去瞅见一下习大大的真容,"伟大的祖国,我爱你~~~&quo ...

  4. m_strcmp

    strcmp比较两个字符串的大小,strcmp(str1, str2); 从str1和str2的第一个元素比较直到出现不同,或者遇到'\0'结束.如果str1 > str2 返回正数,str1 ...

  5. 阿里云上遇到: virtual memory exhausted: Cannot allocate memory

    # dd if=/dev/zero of=/swap bs=1024 count=1M Format the swap file: # mkswap /swap Enable the swap fil ...

  6. super关键字小结(构造方法的执行是不是一定会创建对象?)

    1.父类 public class Person { private String name = "李四"; private int age; public Person() { ...

  7. Wireshark远程抓包

    1.被监控主机上,在安装目录下点击rpcapd.exe,运行server服务,并关闭防火墙 2.监控机器上,Capture Options,点击Manage Interfaces,弹出如下对话框 选择 ...

  8. Ftp客户端需要TSL功能的文件上传

    Ftp客户端需要TSL功能 1.由于最近做了一个项目,需要把打包的文件传输到对方的FTP服务器上,但是用普通的java连接ftp客户端总是连接不上去,对方却说ftp客户端需要开通TSL功能. 直接上代 ...

  9. scala 延迟加载

  10. [转]Linux下的常见信号总结

    转自 https://www.cnblogs.com/gaorong/p/6430905.html 在linux下有很多信号,按可靠性分为可靠信号和非可靠信号,按时间分为实时信号和非实时信号,linu ...