unordered_map find
时间: 2023-10-17 20:37:40 浏览: 107
unordered_map is a container in C++ that stores elements in an unordered manner based on a hash function. To find an element in an unordered_map, you can use the find() function. Here's an example:
```cpp
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, std::string> map = {{1, "apple"}, {2, "banana"}, {3, "orange"}};
auto it = map.find(2);
if (it != map.end()) {
std::cout << "Key found! Value is: " << it->second << std::endl;
} else {
std::cout << "Key not found! << std::endl;
}
return 0;
}
```
In this example, we create an unordered_map called `map` with integer keys and string values. We then use the find() function to search for the key 2. If the key is found, it returns an iterator pointing to the key-value pair, and we can access the value using `it->second`. If the key is not found, it returns `map.end()`.
Hope this helps! Let me know if you have any more questions.
阅读全文