阮一峰的个人网站,zencart中文网站,wordpress内容构建器,事件营销的案例有哪些朋友们、伙计们#xff0c;我们又见面了#xff0c;本期来给大家解读一下有关unordered系列关联式容器的知识点#xff0c;如果看完之后对你有一定的启发#xff0c;那么请留下你的三连#xff0c;祝大家心想事成#xff01; C 语 言 专 栏#xff1a;C语言#xff1a;… 朋友们、伙计们我们又见面了本期来给大家解读一下有关unordered系列关联式容器的知识点如果看完之后对你有一定的启发那么请留下你的三连祝大家心想事成 C 语 言 专 栏C语言从入门到精通 数据结构专栏数据结构 个 人 主 页 stackY、 C 专 栏 C Linux 专 栏 Linux 目录 1. unordered系列关联式容器
2. unordered_map的介绍
2.1 文档介绍
2.2 接口介绍 1. unordered系列关联式容器 在C98中STL提供了底层为红黑树结构的一系列关联式容器在查询时效率可达到即最差情况下需要比较红黑树的高度次当树中的节点非常多时查询效率也不理想。最好的查询是进行很少的比较次数就能够将元素找到因此在C11中STL又提供了4个unordered系列的关联式容器这四个容器与红黑树结构的关联式容器使用方式基本类似只是其底层结构不同本文中只对unordered_map和unordered_set进行介绍 unordered_multimap和unordered_multiset可查看文档介绍unordered_multimap文档详细介绍https://legacy.cplusplus.com/reference/unordered_map/unordered_multimap/ unordered_multiset文档详细介绍https://legacy.cplusplus.com/reference/unordered_set/unordered_multiset/ 2. unordered_map的介绍 2.1 文档介绍 unordered_map文档详细介绍https://legacy.cplusplus.com/reference/unordered_map/unordered_map/ 1. unordered_map是存储key, value键值对的关联式容器其允许通过key快速的索引到与其对应的value。2. 在unordered_map中键值通常用唯一的标识元素而映射值是一个对象其内容与此键关联。键和映射值的类型可能不同。3. 在内部,unordered_map没有对kye, value按照任何特定的顺序排序, 为了能在常数范围内找到key所对应的valueunordered_map将相同哈希值的键值对放在相同的桶中。4. unordered_map容器通过key访问单个元素要比map快但它通常在遍历元素子集的范围迭代方面效率较低。5. unordered_maps实现了直接访问操作符(operator[])它允许使用key作为参数直接访问value。6. 它的迭代器至少是前向迭代器。 2.2 接口介绍 1. unordered_map的构造 函数声明功能介绍unordered_map构造不同格式的unordered_map对象 2. unordered_map的容量 函数声明功能介绍bool empty() const检测unordered_map是否为空size_t size() const获取unordered_map的有效元素个数 void test_unordered_map1()
{unordered_mapstring, string _dirt;_dirt.insert(make_pair(sort,排序));_dirt.insert(make_pair(erase, 删除));_dirt.insert(make_pair(insert, 插入));_dirt.insert(make_pair(print, 打印));if (!_dirt.empty()){cout _dirt.size() endl;}for (auto ret : _dirt){cout ret.first : ret.second endl;}
} 3. unordered_map的迭代器 函数声明功能介绍begin返回unordered_map第一个元素的迭代器end返回unordered_map最后一个元素下一个位置的迭代器cbegin返回unordered_map第一个元素的const迭代器cend返回unordered_map最后一个元素下一个位置的const迭代器 void test_unordered_map2()
{unordered_mapstring, string _dirt;_dirt.insert(make_pair(sort, 排序));_dirt.insert(make_pair(erase, 删除));_dirt.insert(make_pair(insert, 插入));_dirt.insert(make_pair(print, 打印));unordered_mapstring, string::iterator it _dirt.begin();//auto it _dirt.begin();while (it ! _dirt.end()){cout it-first : it-second endl;it;}
} 4. unordered_map的元素访问 函数声明功能介绍operator[]返回与key对应的value没有一个默认值 注意该函数中实际调用哈希桶的插入操作用参数key与V()构造一个默认值往底层哈希桶 中插入如果key不在哈希桶中插入成功返回V()插入失败说明key已经在哈希桶中 将key对应的value返回与map的operator[]用法一致。 void test_unordered_map3()
{string arr[] { 香蕉, 甜瓜,苹果, 西瓜, 苹果, 西瓜, 苹果, 苹果, 西瓜, 苹果, 香蕉, 苹果, 香蕉 };unordered_mapstring, int CountMap;for (auto str : arr){CountMap[str];}for (auto e : CountMap){cout e.first : e.second endl;}
} 5. unordered_map的查询 函数声明功能介绍iterator find(const K key)返回key在哈希桶中的位置size_t count(const K key)返回哈希桶中关键码为key的键值对的个数 注意unordered_map中key是不能重复的因此count函数的返回值最大为1 6. unordered_map的修改操作 函数声明功能介绍insert向容器中插入键值对erase删除容器中的键值对void clear()清空容器中有效元素个数void swap(unordered_map)交换两个容器中的元素 void test_unordered_map4()
{unordered_mapstring, string _dirt;unordered_mapstring, string ump;_dirt.insert(make_pair(sort, 排序));_dirt.insert(make_pair(erase, 删除));_dirt.insert(make_pair(insert, 插入));_dirt.insert(make_pair(print, 打印));_dirt.erase(print);_dirt.swap(ump);for (auto e : ump){cout e.first : e.second endl;}
} 7. unordered_map的桶操作 函数声明功能介绍size_t bucket_count()const返回哈希桶中桶的总个数size_t bucket_size(size_t n)const返回n号桶中有效元素的总个数size_t bucket(const K key)返回元素key所在的桶号 unordered_set就不做介绍了其接口与map用法相同。 unordered_set文档详细介绍https://legacy.cplusplus.com/reference/unordered_set/unordered_set/ 朋友们、伙计们美好的时光总是短暂的我们本期的的分享就到此结束欲知后事如何请听下回分解~最后看完别忘了留下你们弥足珍贵的三连喔感谢大家的支持