技术在于交流、沟通,本文为博主原创文章转载请注明出处并保持作品的完整性

unordered_set与与unordered_map相似,这次主要介绍unordered_set

unordered_set它的实现基于hashtable,它的结构图仍然可以用下图表示,这时的空白格不在是单个value,而是set中的key与value的数据包

有unordered_set就一定有unordered_multiset.跟set和multiset一样,一个key可以重复一个不可以

unordered_set是一种无序集合,既然跟底层实现基于hashtable那么它一定拥有快速的查找和删除,添加的优点.基于hashtable当然就失去了基于rb_tree的自动排序功能

unordered_set无序,所以在迭代器的使用上,set的效率会高于unordered_set

template<class _Value,
class _Hash = hash<_Value>,
class _Pred = std::equal_to<_Value>,
class _Alloc = std::allocator<_Value> >
class unordered_set
: public __unordered_set<_Value, _Hash, _Pred, _Alloc>
{
typedef __unordered_set<_Value, _Hash, _Pred, _Alloc> _Base; ... }  

参数1 _Value key和value的数据包

参数2 _Hash hashfunc获取hashcode的函数

参数3 _Pred 判断key是否相等

参数4 分配器

下面介绍一下unordered_set的基本使用,最后我会分享一下我的测试代码


一 定义

    //定义
unordered_set<int> c1; //operator=
unordered_set<int> c2;
c2 = c1;

二 容量操作

//判断是否为空
c1.empty(); //获取元素个数 size()
c1.size(); //获取最大存储量 max_size()
c1.max_size();

三 迭代器操作

    //返回头迭代器 begin()
unordered_set<int>::iterator ite_begin = c1.begin(); //返回尾迭代器 end()
unordered_set<int>::iterator ite_end = c1.end(); //返回const头迭代器 cbegin()
unordered_set<int>::const_iterator const_ite_begin = c1.cbegin(); //返回const尾迭代器 cend()
unordered_set<int>::const_iterator const_ite_end = c1.cend(); //槽迭代器
unordered_set<int>::local_iterator local_iter_begin = c1.begin();
unordered_set<int>::local_iterator local_iter_end = c1.end();

四 基本操作

    //查找函数 find() 通过给定主键查找元素
unordered_set<int>::iterator find_iter = c1.find(); //value出现的次数 count() 返回匹配给定主键的元素的个数
c1.count(); //返回元素在哪个区域equal_range() 返回值匹配给定搜索值的元素组成的范围
pair<unordered_set<int>::iterator, unordered_set<int>::iterator> pair_equal_range = c1.equal_range(); //插入函数 emplace()
c1.emplace(); //插入函数 emplace_hint() 使用迭代器
c1.emplace_hint(ite_begin, ); //插入函数 insert()
c1.insert(); //删除 erase()
c1.erase();//1.迭代器 value 区域 //清空 clear()
c1.clear(); //交换 swap()
c1.swap(c2);

五 篮子操作

    //篮子操作 篮子个数 bucket_count() 返回槽(Bucket)数
c1.bucket_count(); //篮子最大数量 max_bucket_count() 返回最大槽数
c1.max_bucket_count(); //篮子个数 bucket_size() 返回槽大小
c1.bucket_size(); //返回篮子 bucket() 返回元素所在槽的序号
c1.bucket(); // load_factor 返回载入因子,即一个元素槽(Bucket)的最大元素数
c1.load_factor(); // max_load_factor 返回或设置最大载入因子
c1.max_load_factor();

六 内存操作

    //    rehash    设置槽数
c1.rehash(); // reserve 请求改变容器容量
c1.reserve();

七 hash func

    //hash_function() 返回与hash_func相同功能的函数指针
auto hash_func_test = c1.hash_function(); //key_eq() 返回比较key值得函数指针
auto key_eq_test = c1.key_eq();

八 测试代码

#include <iostream>
#include <unordered_set>
using namespace std; namespace wzj001{
void coutUnorderedSet(std::unordered_set<int>& m, string funcName) {
std::unordered_set<int>::iterator it;
std::cout << funcName << ": ";
for ( it = m.begin(); it != m.end(); it++ )
std::cout << *it << " ";
std::cout << std::endl;
} void initUnorderSet(unordered_set<int>& tmp)
{
for(int i = ; i < ; i++)
tmp.insert(i);
} string turnBoolToString(bool tmp)
{
return tmp ? "true" : "false";
} void basicOperationUnorderedSet()
{
//定义
std::unordered_set<int> c;
// 普通插入,返回pair<迭代器,插入是否成功>
pair<unordered_set<int>::iterator, bool> c_insert = c.insert();
cout << "指向key的迭代器: " << *c_insert.first << " 插入是否成功 "<< turnBoolToString(c_insert.second)<<endl;
pair<unordered_set<int>::iterator, bool> c_insert2 = c.insert();
cout << "指向key的迭代器: " << *c_insert2.first << " 插入是否成功 "<< turnBoolToString(c_insert2.second)<<endl;
pair<unordered_set<int>::iterator, bool> c_insert3 = c.insert();
cout << "指向key的迭代器: " << *c_insert3.first << " 插入是否成功 "<< turnBoolToString(c_insert3.second)<<endl; //按指定区域插入
std::unordered_set<int> c_insert_region;
c_insert_region.insert(c.begin(), c.end());
coutUnorderedSet(c_insert_region, "按指定区域插入"); //构造插入
std::unordered_set<int> c_emplace;
c_emplace.emplace();
c_emplace.emplace();
c_emplace.emplace();
coutUnorderedSet(c_emplace, "构造插入"); //迭代器插入
std::unordered_set<int> c_emplace_hint;
c_emplace_hint.emplace_hint(c_emplace_hint.begin(), );
c_emplace_hint.emplace_hint(c_emplace_hint.begin(), );
c_emplace_hint.emplace_hint(c_emplace_hint.begin(), );
coutUnorderedSet(c_emplace_hint, "迭代器插入"); //删除
std::unordered_set<int> c_erase;
initUnorderSet(c_erase);
coutUnorderedSet(c_erase, "初始化c_erase");
//指定位置删除
c_erase.erase(c_erase.begin());
coutUnorderedSet(c_erase, "指定位置删除"); //指定key删除
c_erase.erase();
coutUnorderedSet(c_erase, "指定key删除"); //指定区域删除
c_erase.erase(c_erase.begin(), c_erase.end());
coutUnorderedSet(c_erase, "指定区域删除"); //交换
c.swap(c_emplace);
coutUnorderedSet(c, "交换"); } void unorderSetElementLookup()
{
//查找
std::unordered_set<int> c_find;
initUnorderSet(c_find);
std::unordered_set<int>::iterator find_iter = c_find.find();
if(find_iter != c_find.end())
{
cout<< "找到元素 : "<< *find_iter << endl;
}
else
cout<< "没找到 !"<< endl; cout << "value出现次数 :" <<c_find.count()<< endl; //set key不可重复 pair<std::unordered_set<int>::iterator, std::unordered_set<int>::iterator> tmp = c_find.equal_range(); if(tmp.first != c_find.end()&& tmp.second != c_find.end())
{
cout << "该值所在区间为[" << *tmp.first << "," << *tmp.second << "]" << endl;
}
} void unorderSetBuckets()
{
//篮子操作
std::unordered_set<int> c_buckets;
initUnorderSet(c_buckets);
cout << "篮子个数: " << c_buckets.bucket_count()<< endl;
cout << "篮子大小: " << c_buckets.bucket_size() << endl;
cout << "最大篮子个数: " << c_buckets.max_bucket_count() << endl;
cout << "该值所在篮子序号: " << c_buckets.bucket() << endl;
} void unorderSetHashPolicy()
{
std::unordered_set<int> c_;
cout << "负载: "<< c_.load_factor()<< endl;
initUnorderSet(c_);
cout << "负载: "<< c_.load_factor()<< endl;//使用的篮子数/篮子总数 默认的篮子数为11
cout << "最大负载: "<< c_.max_load_factor() << endl;
c_.reserve();//预设篮子数 ,但是还没有设定
c_.rehash();//设定篮子数
} void unorderSetObservers()
{
std::unordered_set<int> c_;
initUnorderSet(c_);
std::unordered_set<int>::hasher xxx = c_.hash_function();
std::unordered_set<int>::key_equal zzz = c_.key_eq();
cout << "hash_func: " << xxx() << endl;
cout << "key_eq: " << turnBoolToString(zzz(,)) << endl;
}
} int main()
{
wzj001::basicOperationUnorderedSet();
wzj001::unorderSetElementLookup();
wzj001::unorderSetBuckets();
wzj001::unorderSetHashPolicy();
wzj001::unorderSetObservers();
}

STL标准库-容器-unordered_set的更多相关文章

  1. STL标准库-容器-set与map

    STL标准库-容器-set与multiset C++的set https://www.cnblogs.com/LearningTheLoad/p/7456024.html STL标准库-容器-map和 ...

  2. STL标准库-容器-set与multiset

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. set与multiset关联容器 结构如下 set是一种关联容器,key即value,value即key.它是自动排序,排序特点依据key se ...

  3. STL标准库-容器-deque

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. deque双向开口可进可出的容器 我们知道连续内存的容器不能随意扩充,因为这样容易扩充别人那去 deque却可以,它创造了内存 ...

  4. STL标准库-容器-vector

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. 向量容器vector是一个动态数组,内存连续,它是动态分配内存,且每次扩张的原来的二倍. 他的结构如下 一 定义 vector ...

  5. STL标准库-容器-list

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. list 表示非连续的内存区域,并通过一对指向首尾元素的指针双向链接起来,从而允许向前和向后两个方向进行遍历.在list 的任 ...

  6. STL标准库-容器适配器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 上一节介绍了仿函数适配器,这节主要介绍容器适配器和迭代器适配器的概念,其实容器适配器和迭代器其适配器就是封装了一些其他class ...

  7. STL标准库-容器-rb_tree

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 红黑树,关联式容器底层实现(map set),在使用中基本运用不到,但是还是想了解一下他的运作方式 Red_Black tree ...

  8. STL标准库-容器-map和multimap

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 map与multimap为关联容器,结构如下 map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此ma ...

  9. STL标准库-容器-deque 双端队列

    头文件: #include<deque> 常用操作: https://www.cnblogs.com/LearningTheLoad/p/7450948.html

随机推荐

  1. Which adidas NMD Singapore is your favorite

    The adidas NMD Singapore just keeps the hits coming this fall with another change that's sure to bec ...

  2. 安装完C++builder6.0启动的时候总是出现无法将'C:\Program Files\Borland\CBuilder6\Bin\bcb.$$$'重命名为bcb.dro

    :兼容性问题  运行前右键属性“兼容性”-尝试不同的兼容性.比如“windows 8”

  3. hdu4758 Walk Through Squares

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=4758 题目: Walk Through Squares Time Limit: 4000/20 ...

  4. numpy模块学习笔记

    # encoding=utf-8 import numpy as np from numpy.linalg import * def main(): # 1.最基本的array lst = [[1, ...

  5. SQL学习笔记四(补充-1)之MySQL单表查询

    阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录 ...

  6. [Pytorch]Pytorch 细节记录(转)

    文章来源 https://www.cnblogs.com/king-lps/p/8570021.html 1. PyTorch进行训练和测试时指定实例化的model模式为:train/eval eg: ...

  7. HDU 1317 XYZZY(floyd+bellman_ford判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始 ...

  8. win10不能上网问题的解决办法

    升级到 Windows 10 以后,可以 ping 通外网,但是浏览器和各种客户端都不能正常访问网络了.百度以后得到如下解决办法: 以管理员身份运行cmd,输入netsh winsock reset后 ...

  9. Sublime编辑器上的Linux Vim插件常用快捷键

    Sublime编辑器上的 Vim 插件常用快捷键 vim 的设置: 在Reference-> User Settings(首选项--> 设置用户) 选项,点击进入配置文件编辑状态,默认的用 ...

  10. R语言笔记-set.seed()函数

    今天查了一下R语言中set.seed(),该命令的作用是设定生成随机数的种子,种子是为了让结果具有重复性.如果不设定种子,生成的随机数无法重现. set.seed()用于设定随机数种子,一个特定的种子 ...