set unordered_set
时间: 2023-11-03 20:04:46 浏览: 109
unordered_set is a container in C++ Standard Template Library (STL) that stores a collection of unique elements. It is an associative container, meaning the elements are stored using a hash table implementation to provide constant-time average complexity for insertion, deletion, and search operations.
To use unordered_set in C++, you need to include the <unordered_set> header file and declare a variable of type unordered_set. Here's an example:
```cpp
#include <unordered_set>
int main() {
std::unordered_set<int> set;
// Insert elements
set.insert(10);
set.insert(20);
set.insert(30);
// Check if an element exists
if (set.find(20) != set.end()) {
std::cout << "Element found!" << std::endl;
}
// Remove an element
set.erase(10);
return 0;
}
```
In this example, we create an unordered_set called `set` to store integers. We insert three elements (10, 20, and 30) using the `insert` function. We then check if the element 20 exists using the `find` function and erase the element 10 using the `erase` function.
阅读全文