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

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

6.查找

(1)find( )

根据k返回k所在位置的迭代器,如果没找到就返回end

iterator find ( const key_type& k );

查找洒水车:

cout << um1.find("洒水车")->second << endl;

(2)count( )

统计容器中key为k的元素的个数:

size_type count ( const key_type& k ) const;

统计um1中key为"搅拌车"的元素个数:

cout << um1.count("搅拌车") << endl;

7.元素修改

(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 << um1.insert(make_pair<string, int>("大货车", 9)).second << endl;//不存在,插入成功
2.  cout << um1.insert(make_pair<string, int>("搅拌车", 1)).second << endl;//已存在,插入失败

 ②返回插入元素的位置

cout << um1.insert(um1.begin(), make_pair<string, int>("扫地车", 10))->second << endl;

③ 插入一段区间

1. unordered_map<string, int> um2(um1.begin(), um1.end());
2.  unordered_map<string, int>::iterator it2 = um2.begin();
3. 
4.  while (it2 != um2.end())
5.  {
6.    cout << it2->first << ":" << it2->second << endl;
7.    it2++;
8.  }
9.  cout << endl;

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

1.     unordered_map<string, int> um3;    
2.     um3.insert({ { "摩托车",3 }, { "电动车",7 }});
3.  unordered_map<string, int>::iterator it3 = um3.begin();
4.  while (it3 != um3.end())
5.  {
6.    cout << it3->first << ":" << it3->second << endl;
7.    it3++;
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位置的元素,并返回删除元素的位置

删除搅拌车:

cout << um1.erase(um1.find("搅拌车"))->first << endl;

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

cout << um1.erase("自行车") << endl;

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

 

cout << um1.erase(um1.find("消防车"), um1.find("扫地车"))->first << endl;

(3)clear( )

清空所有元素:

void clear() noexcept;

清空um1中所有元素:

um1.clear();

(4)swap( )

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

1.  unordered_map<string, string> um4;
2.  um4.insert(make_pair<string, string>("spring", "春天"));
3.  um4.insert(make_pair<string, string>("summer", "夏天"));
4.  um4.insert(make_pair<string, string>("autumn", "秋天"));
5.  um4.insert(make_pair<string, string>("winter", "冬天"));
6. 
7.  unordered_map<string, string> um5;
8.  um5.insert(make_pair<string, string>("east", "东"));
9.  um5.insert(make_pair<string, string>("south", "南"));
10.   um5.insert(make_pair<string, string>("west", "西"));
11.   um5.insert(make_pair<string, string>("north", "北"));
12. 
13.   um4.swap(um5);
14. 
15.   unordered_map<string, string>::iterator it4 = um4.begin();
16.   while (it4 != um4.end())
17.   {
18.     cout << it4->first << ":" << it4->second << endl;
19.     it4++;
20.   }
21.   cout << endl;
22. 
23.   unordered_map<string, string>::iterator it5 = um5.begin();
24.   while (it5 != um5.end())
25.   {
26.     cout << it5->first << ":" << it5->second << endl;
27.     it5++;
28.   }
29.   cout << endl;

 

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

相关文章
|
18小时前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
18小时前
|
设计模式 C语言 C++
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
|
18小时前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
18小时前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
1天前
|
存储 算法 C语言
c++的学习之路:9、STL简介与string(1)
c++的学习之路:9、STL简介与string(1)
11 0
|
12天前
|
存储 算法 C++
【C++初阶】STL详解(九) priority_queue的使用与模拟实现
【C++初阶】STL详解(九) priority_queue的使用与模拟实现
20 0
|
12天前
|
存储 算法 编译器
【C++初阶】STL详解(三)vector的介绍与使用
【C++初阶】STL详解(三)vector的介绍与使用
34 0
|
18小时前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
1天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
16 0
|
1天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
13 0

热门文章

最新文章