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

 

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

相关文章
|
5天前
|
安全 Linux 编译器
从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)(下)
从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)
16 0
|
5天前
|
安全 C语言 C++
从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)(中)
从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)
13 0
|
5天前
|
Linux 调度 C语言
从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)(上)
从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)
14 0
|
5天前
|
存储 算法 C语言
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系(下)
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系
20 5
|
5天前
|
算法 C语言 C++
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系(上)
从C语言到C++_38(C++的IO流+空间适配器)STL六大组件联系
13 0
|
3天前
|
存储 Serverless 数据安全/隐私保护
C++ 类的成员函数和数据成员的技术性探讨
C++ 类的成员函数和数据成员的技术性探讨
12 0
|
7天前
|
存储 编译器 程序员
c++存储类
c++存储类
26 3
|
7天前
|
C++
c++类&对象
c++类&对象
21 3
|
11天前
|
C++
C++派生类
C++派生类
26 0
|
11天前
|
存储 程序员 数据安全/隐私保护
C++类
C++类
24 0