example4_unordered_list_hash_tables.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <unordered_set>
#include <string>

int main() {
    std::unordered_set<std::string> colors = {"red", "green", "blue"};

    // Inserting elements
    colors.insert("yellow");

    // Checking for existence
    if (colors.find("purple") == colors.end()) {
        std::cout << "Purple is not in the set" << std::endl;
    }

    // Removing an element
    colors.erase("green");

    // Iterating through the set
    for (const auto& color : colors) {
        std::cout << color << " ";
    }
    std::cout << std::endl;

    // Size of the set
    std::cout << "Number of colors: " << colors.size() << std::endl;

    return 0;
}
Back to containers