C++ container classes<unordered_set>

In C++,<unordered_set>is part of the Standard Template Library (STL), providing a hash-table-based container for storing a collection of unique elements.

andsetDifferent,unordered_setdoes not guarantee the ordering of elements, but typically provides faster lookup, insertion, and deletion operations.

unordered_setis a template class, defined as follows:

#include <unordered_set>

std::unordered_set<Key, Hash = std::hash<Key>, Pred = std::equal_to<Key>, Alloc = std::allocator<Key>>
  • Keyis stored inunordered_setthe element types in.
  • Hashis a function or function object used to generate the hash value of an element; the default isstd::hash<Key>。
  • Predis a binary predicate used to compare whether two elements are equal; the default isstd::equal_to<Key>。
  • Allocis the allocator type, used to manage memory allocation; the default isstd::allocator<Key>。

Syntax

The following are some basicunordered_setOperations:

  • Constructorcreate an emptyunordered_set。

    std::unordered_set<int> uset;
  • Insert element: Useinsert()method.

    uset.insert(10);
  • Find element: Usefind()method.

    auto it = uset.find(10);
    if (it != uset.end()) {
      // 元素存在
    }
  • Delete element: Useerase()method.

    uset.erase(10);
  • size and empty check: Usesize()andempty()method.

    size_t size = uset.size();
    bool isEmpty = uset.empty();
  • Clear the container: Useclear()method.

    uset.clear();

Example

Below is an example usingunordered_seta simple example, including the output.

Example

#include <iostream>
#include <unordered_set>

int main() {
    // Create an integer type unordered_set
    std::unordered_set<int> uset;

    // Insert elements
    uset.insert(10);
    uset.insert(20);
    uset.insert(30);

    // Print the elements in the unordered_set
    std::cout << "Elements in uset: ";
    for (int elem : uset) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    // Find element
    auto it = uset.find(20);
    if (it != uset.end()) {
        std::cout << "Element 20 found in uset." << std::endl;
    } else {
        std::cout << "Element 20 not found in uset." << std::endl;
    }

    // Delete elements
    uset.erase(20);
    std::cout << "After erasing 20, elements in uset: ";
    for (int elem : uset) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    // Check size and whether it is empty
    std::cout << "Size of uset: " << uset.size() << std::endl;
    std::cout << "Is uset empty? " << (uset.empty() ? "Yes" : "No") << std::endl;

    // Clear the unordered_set
    uset.clear();
    std::cout << "After clearing, is uset empty? " << (uset.empty() ? "Yes" : "No") << std::endl;

    return 0;
}

Output:

Elements in uset: 10 20 30 
Element 20 found in uset.
After erasing 20, elements in uset: 10 30 
Size of uset: 2
Is uset empty? No
After clearing, is uset empty? Yes

unordered_setis a very useful container, especially suitable for scenarios that require fast lookup, insertion, and deletion operations, while not requiring element ordering.

other extensions