【C++】-- STL之unordered_map/unordered_set详解(二)

简介: 【C++】-- STL之unordered_map/unordered_set详解

6.元素修改

(1)insert( )

1. pair<iterator,bool> insert ( const value_type& val );//插入元素,成功返回的pair的第二个元素为true,失败则为false
2. iterator insert ( const_iterator hint, const value_type& val );//返回插入元素的位置
3. template <class InputIterator>
4. void insert ( InputIterator first, InputIterator last );//插入一段区间
5. void insert ( initializer_list<value_type> il );//将列表作为元素插入容器中

①插入元素

1.  cout << us2.insert(568).second << endl;//不存在,插入成功
2.  cout << us2.insert(291).second << endl;//已存在,插入失败

 

②返回插入元素的位置

cout << *us2.insert(us3.begin(), 65) << endl;

③ 插入一段区间

1.     unordered_set<int> us2;
2.  us2.insert(us1.begin(), us1.end());
3.  unordered_set<int>::iterator it = us2.begin();
4.  while (it != us2.end())
5.  {
6.    cout << *it << " ";
7.    it++;
8.  }
9.  cout << endl;

 

④将列表作为元素插入容器中

1.  unordered_set<string> us4;
2.  us4.insert({ "int", "string", "float" });
3.  unordered_set<string>::iterator it = us4.begin();
4.  while (it != us4.end())
5.  {
6.    cout << *it << " ";
7.    it++;
8.  }
9.  cout << endl;

(2)erase( )

删除元素:

1. iterator erase ( const_iterator position );//删除position位置的元素,并返回删除元素的位置
2. size_type erase ( const key_type& k );//返回删除值为k的元素的个数
3. iterator erase ( const_iterator first, const_iterator last );//删除从first到last区间的元素,并返回删除的last元素的位置

①删除position位置的元素,并返回删除元素的位置

1.  unordered_set<int> us2;
2.  us2.insert(us1.begin(), us1.end());
3.  cout << *us2.erase(us2.find(6)) << endl;

② 删除值为k的元素的,k存在返回1,k不存在返回0

cout << us2.erase(72) << endl;

③ 删除从first到last区间的元素,并返回删除的last元素的位置

cout << *us2.erase(us2.find(6), us2.find(291)) << endl;

(3)clear( )

删除容器中所有元素

void clear() noexcept;

清空us2中所有元素:

us2.clear();

(4)swap( )

交换两个同类型容器中的元素

1. unordered_set<int> us1;
2.  us1.insert(2);
3.  us1.insert(72);
4.  us1.insert(6);
5.  us1.insert(35);
6.  us1.insert(291);
7.  us1.insert(327);
8. 
9.  unordered_set<int> us5;
10.   us5.insert(56);
11.   us5.insert(57);
12.   us5.insert(58);
13.   us5.insert(59);
14.   us5.insert(60);
15.   us5.insert(61);
16. 
17.   us1.swap(us5);
18. 
19.   unordered_set<int>::iterator it1 = us1.begin();
20.   while (it1 != us1.end())
21.   {
22.     cout << *it1 << " ";
23.     it1++;
24.   }
25.   cout << endl;
26. 
27.   unordered_set<int>::iterator it5 = us5.begin();
28.   while (it5 != us5.end())
29.   {
30.     cout << *it5 << " ";
31.     it5++;
32.   }
33.   cout << endl;

哈系桶和哈希策略的函数等介绍完哈希表之后才能理解。

三、 unordered_map

1.特点

(1)unordered_map是存储<key, value>键值对的关联式容器,允许通过key快速的索引到与其对应的value。

(2)在unordered_map中,键值通常用于惟一地标识元素,而映射value是一个对象,其内容与key关联。键和映射值的类型可能不同。

(3)unordered_map没有对<kye, value>按照任何特定的顺序排序, 为了能在常数范围内找到key所对应的value,unordered_map将相同哈希值的键值对放在相同的桶中。

(4)unordered_map容器通过key访问单个元素要比map快,但它通常在遍历元素子集的范围迭代方面效率较低。

(5)unordered_map实现了直接访问操作符(operator[]),它允许使用key作为参数直接访问value。

(6)容器中的迭代器至少有正向迭代器。

2.构造  

1. explicit unordered_map ( size_type n = /* see below */,
2. const hasher& hf = hasher(),
3. const key_equal& eql = key_equal(),
4. const allocator_type& alloc = allocator_type() );//构造空的unordered_map对象
5. 
6. template <class InputIterator>
7. unordered_map ( InputIterator first, InputIterator last,
8.                   size_type n = /* see below */,
9. const hasher& hf = hasher(),
10. const key_equal& eql = key_equal(),
11. const allocator_type& alloc = allocator_type() );//用迭代器范围构造unordered_map对象
12. 
13. unordered_map ( const unordered_map& ump );//拷贝构造一个unordered_map对象

(1)构造一个空的unordered_map对象

unordered_map<string, int> um1;

向里面插入元素:

1.     um1.insert(make_pair<string, int>("自行车", 8));
2.  um1.insert(make_pair<string, int>("消防车", 1));
3.  um1.insert(make_pair<string, int>("洒水车", 6));
4.  um1.insert(make_pair<string, int>("搅拌车", 7));
5.  um1.insert(make_pair<string, int>("小汽车", 5));

(2)用迭代器范围构造unordered_set对象

用um1的迭代器范围构造um2:

unordered_map<string, int> um2(um1.begin(), um1.end());

(3)拷贝构造一个unordered_set对象

用um2拷贝构造um3:

unordered_map<string, int> um3(um2);

3.容量

(1)empty( )

判断unordered_map是否为空:

bool empty() const noexcept;

判断um1是否为空:

cout << um1.empty() << endl;

(2)size( )

返回unordered_map中的元素个数:

size_type size() const noexcept;

求um1中元素的个数:

cout << um1.size() << endl;

(3)max_size( )

返回 unordered_map可存储的最大元素个数:

size_type max_size() const noexcept;

求um1最大元素个数 :

cout << um1.max_size() << endl;

 

4.迭代器

(1)begin( )

返回迭代器开始:

iterator begin() noexcept;

返回um1迭代器开始:

unordered_map<string, int>::iterator it = um1.begin();

(2)end( )

返回迭代器结尾:

iterator end() noexcept;

返回um1迭代器结尾:

um1.end();

5.元素操作符[ ]

访问key为k的元素,如果存在就返回value的引用:

mapped_type& operator[] ( const key_type& k );

访问key为小汽车的元素,并返回小汽车对应的value的引用:

cout << um1["小汽车"] << endl;

相关文章
|
8月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
238 2
|
8月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
465 73
|
5月前
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
260 0
|
8月前
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
369 3
|
9月前
|
编译器 容器
哈希表模拟封装unordered_map和unordered_set
哈希表模拟封装unordered_map和unordered_set
|
存储 算法 C++
【C++】unordered_map(set)
C++中的`unordered`容器(如`std::unordered_set`、`std::unordered_map`)基于哈希表实现,提供高效的查找、插入和删除操作。哈希表通过哈希函数将元素映射到特定的“桶”中,每个桶可存储一个或多个元素,以处理哈希冲突。主要组成部分包括哈希表、哈希函数、冲突处理机制、负载因子和再散列,以及迭代器。哈希函数用于计算元素的哈希值,冲突通过开链法解决,负载因子控制哈希表的扩展。迭代器支持遍历容器中的元素。`unordered_map`和`unordered_set`的插入、查找和删除操作在理想情况下时间复杂度为O(1),但在冲突较多时可能退化为O(n)。
243 5
|
存储 C++ 容器
【C++】map的模拟实现
C++中的`map`是STL中的一种关联容器,存储键值对且键唯一。`map`基于红黑树实现,自动按键排序,支持动态调整、复杂数据类型、丰富的成员函数及双向迭代器。插入、查找等操作保证了对数时间复杂度,适用于需要快速查找和有序存储的场景。
169 3
|
1月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
162 1
|
4月前
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
392 1
|
1月前
|
存储 算法 容器
set_map的实现+set/map加持秒杀高频算法题锻炼算法思维
`set`基于红黑树实现,支持有序存储、自动去重,增删查效率为O(logN)。通过仿函数可自定义排序规则,配合空间配置器灵活管理内存。不支持修改元素值,迭代器失效需注意。`multiset`允许重复元素。常用于去重、排序及查找场景。

热门文章

最新文章

下一篇
oss云网关配置