example2_set_operations.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <set>

int main() {
    std::set<int> set1 = {1, 2, 3, 4, 5};
    std::set<int> set2 = {4, 5, 6, 7, 8};

    // Find element
    auto it = set1.find(3);
    if (it != set1.end()) {
        std::cout << "Found: " << *it << std::endl;
    }

    // Erase element
    set1.erase(4);

    // Check if element exists
    if (set1.count(4) == 0) {
        std::cout << "4 is not in the set" << std::endl;
    }

    return 0;
}
Back to set