一:无序容器简介

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. STM32 LoRaWAN探索板B-L072Z-LRWAN1中文用户手册

    UM2115用户手册 支持LoRaWAN和 LPWAN协议的STM32L0探索套件 前言 B-L072Z-LRWAN1探索套件采用了 Murata公司的CMWX1ZZABZ-091 LoRa模块.该探 ...

  2. nextjs —— jsx style 学习记录

    作用域 全局 <style global jsx>{` .hero { width: 100%; color: #333; } .title { margin: 0; width: 100 ...

  3. Mybatis之foreach批量插入

    1接口 public boolean insertMembersBatch(@Param("memberList") List<Members> members); @ ...

  4. PHP搭建大文件切割分块上传功能示例

    转载:https://www.jb51.net/article/101931.htm 背景 在网站开发中,文件上传是很常见的一个功能.相信很多人都会遇到这种情况,想传一个文件上去,然后网页提示“该文件 ...

  5. 一次vaccum导致的事故

    1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...

  6. LightOJ-1010-Knights in Chessboard(数学)

    链接: https://vjudge.net/problem/LightOJ-1010 题意: Given an m x n chessboard where you want to place ch ...

  7. [Javascript] Construct a Regex to Match Twitter Mentions with Regexr

    regexr is a powerful tool for debugging existing regexes and creating new ones. In this lesson, we'l ...

  8. guava字符串工具 Splitter 主要功能是拆分字符串为集合 Map

    public class SplitterTest { public static void main(String args[]){ //1.拆分字符串为List集合 String str=&quo ...

  9. Ubuntu 18.04安装fcitx输入法

    1.卸载ibus及所有组件 ----------------------------------------------------------------------------------- ro ...

  10. leetcode解题报告(17):Missing Number

    描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mis ...