📜  C++ STL-Set(1)

📅  最后修改于: 2023-12-03 15:29:50.584000             🧑  作者: Mango

C++ STL Set

Set is a container that stores unique elements following a specific order. The set is implemented using a balanced binary search tree. The time complexity of various operations on a set is O(log(n)) in the worst case.

Example

#include <iostream>
#include <set>

int main() {
    std::set<int> numbers;

    // Add elements to the set
    numbers.insert(10);
    numbers.insert(20);
    numbers.insert(30);

    // Remove an element
    numbers.erase(20);

    // Check if the set contains an element
    if (numbers.find(10) != numbers.end()) {
        std::cout << "Element 10 found" << std::endl;
    }

    // Print all elements in the set
    for (int number : numbers) {
        std::cout << number << std::endl;
    }

    return 0;
}
Initialization

A set can be initialized in several ways:

// Using default constructor
std::set<int> numbers;

// Initializing with elements
std::set<int> numbers = { 10, 20, 30 };

// Initializing using the range of iterators
std::vector<int> vector = { 10, 20, 30 };
std::set<int> numbers(vector.begin(), vector.end());
Adding elements

Elements can be added to the set using the insert method:

std::set<int> numbers;
numbers.insert(10);
numbers.insert(20);
numbers.insert(30);
Removing elements

Elements can be removed from the set using the erase method:

std::set<int> numbers = { 10, 20, 30 };
numbers.erase(20);
Checking elements

We can check if a set contains an element using the find method:

std::set<int> numbers = { 10, 20, 30 };
if (numbers.find(10) != numbers.end()) {
    std::cout << "Element 10 found" << std::endl;
}
Iterating over elements

We can iterate over all the elements in the set using a range-based for loop:

std::set<int> numbers = { 10, 20, 30 };
for (int number : numbers) {
    std::cout << number << std::endl;
}
Operations on sets

The set supports various operations such as union, intersection, difference, and symmetric difference. These operations can be carried out using the set algorithms:

std::set<int> set1 = { 1, 2, 3 };
std::set<int> set2 = { 3, 4, 5 };

// Union
std::set<int> union_set;
std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(),
                std::inserter(union_set, union_set.begin()));

// Intersection
std::set<int> intersection_set;
std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(),
                       std::inserter(intersection_set, intersection_set.begin()));

// Difference
std::set<int> difference_set;
std::set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(),
                     std::inserter(difference_set, difference_set.begin()));

// Symmetric difference
std::set<int> symmetric_difference_set;
std::set_symmetric_difference(set1.begin(), set1.end(), set2.begin(), set2.end(),
                               std::inserter(symmetric_difference_set, symmetric_difference_set.begin()));

Conclusion

Set is a useful container in C++ STL for managing unique elements in a specific order. It provides a fast search and retrieval of elements using balanced binary search tree for its implementation.