【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;

相关文章
|
14天前
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
C++STL底层原理:探秘标准模板库的内部机制
|
7月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
191 2
|
7月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
362 73
|
4月前
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
195 0
|
6天前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
44 1
|
3月前
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
282 1
|
4月前
|
存储 JavaScript 前端开发
for...of循环在遍历Set和Map时的注意事项有哪些?
for...of循环在遍历Set和Map时的注意事项有哪些?
270 121
|
4月前
|
编译器 C++ 容器
用一棵红黑树同时封装出map和set
再完成上面的代码后,我们的底层代码已经完成了,这时候已经是一个底层STL的红黑树了,已经已符合库里面的要求了,这时候我们是需要给他穿上对应的“衣服”,比如穿上set的“衣服”,那么这个穿上set的“衣服”,那么他就符合库里面set的要求了,同样map一样,这时候我们就需要实现set与map了。因此,上层容器map需要向底层红黑树提供一个仿函数,用于获取T当中的键值Key,这样一来,当底层红黑树当中需要比较两个结点的键值时,就可以通过这个仿函数来获取T当中的键值了。我们就可以使用仿函数了。
51 0
|
4月前
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
206 0