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

相关文章
|
10天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
17 2
|
11天前
|
存储 算法 C语言
【C++初阶】8. STL初阶 + String类
【C++初阶】8. STL初阶 + String类
47 1
|
22天前
|
算法 C++ 容器
【C++练级之路】【Lv.10】【STL】priority_queue类和反向迭代器的模拟实现
【C++练级之路】【Lv.10】【STL】priority_queue类和反向迭代器的模拟实现
|
22天前
|
C++ 容器
【C++练级之路】【Lv.9】【STL】stack类和queue类的模拟实现
【C++练级之路】【Lv.9】【STL】stack类和queue类的模拟实现
|
24天前
|
存储 算法 C语言
【C++入门到精通】C++入门 —— map & multimap (STL)
之前我们学习了C++的基础和一些概念,现在将探讨重要的STL组件——map与multimap。map是关联容器,提供有序键值对存储,基于红黑树,支持高效查找、插入和删除。每个键唯一对应一个值。multimap则允许键的重复。两者都提供迭代器支持,但map的键是唯一的,而multimap允许键重复,插入和查找效率不同。更多详情,请查阅官方文档。祝学习愉快!
12 0
|
24天前
|
存储 算法 C++
【C++ map结构 】std::map 和 std::unordered_map 在使用上的差异
【C++ map结构 】std::map 和 std::unordered_map 在使用上的差异
20 0
|
24天前
|
算法 安全 编译器
C++:模版初阶 | STL简介
C++:模版初阶 | STL简介
|
28天前
|
存储 算法 C++
【C++ 包装器类 map】C++ 标准库(std)中的map结构 哈希表(unordered_map)和黑红树(map)教程
【C++ 包装器类 map】C++ 标准库(std)中的map结构 哈希表(unordered_map)和黑红树(map)教程
78 1
|
21天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
31 0
|
21天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
30 0

热门文章

最新文章