C++ unordered_map find
时间: 2023-11-06 07:09:08 浏览: 186
unordered_map 是 C++ STL 中的一个关联容器,用于存储键值对。可以使用 find() 函数在 unordered_map 中查找指定键是否存在。如果存在,则返回指向该键值对的迭代器;否则返回 unordered_map::end()。
示例代码:
```
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, int> mymap = {{"apple", 1}, {"banana", 2}, {"orange", 3}};
auto it = mymap.find("banana");
if (it != mymap.end()) {
cout << "banana is found, value is " << it->second << endl;
} else {
cout << "banana is not found" << endl;
}
return 0;
}
```
输出结果:
```
banana is found, value is 2
```
相关问题
c++ unordered_map find复杂度
根据引用\[1\]中的信息,C++的unordered_map是一种关联式容器,其底层结构不同于红黑树结构的关联式容器。在C++11中引入的unordered_map使用哈希表作为底层结构,因此其查询操作的复杂度是常数时间O(1),即不受元素数量的影响。这意味着在unordered_map中使用find函数进行查找操作的复杂度也是常数时间O(1)。所以,unordered_map的find操作的复杂度是O(1)。
#### 引用[.reference_title]
- *1* *2* [C++ unordered_map和unordered_set的使用](https://blog.csdn.net/qq_61635026/article/details/126857258)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [C++ unordered_map](https://blog.csdn.net/m0_67393619/article/details/124503669)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
C++unordered_map的find
C++中的unordered_map是一种关联容器,它提供了快速的查找和插入操作。其中,find函数用于在unordered_map中查找指定键值对应的元素。
find函数的语法如下:
```
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
```
其中,第一个版本返回一个迭代器,指向键值为k的元素。如果在unordered_map中没有找到该元素,则返回指向unordered_map尾部的迭代器。第二个版本是const版本,返回的迭代器不能用于修改元素。
下面是一个使用unordered_map的例子:
```c++
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, int> umap = {{"apple", 1}, {"banana", 2}, {"orange", 3}};
auto it = umap.find("apple");
if (it != umap.end()) {
cout << "apple is found, value is " << it->second << endl;
} else {
cout << "apple is not found" << endl;
}
return 0;
}
```
输出结果为:
```
apple is found, value is 1
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)