WikiGalaxy

Personalize

C++ Set

Introduction to C++ Set:

A set in C++ is a part of the Standard Template Library (STL) and is used to store unique elements. It is an associative container that automatically orders its elements.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet;
    mySet.insert(1);
    mySet.insert(2);
    mySet.insert(3);
    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Key Characteristics:

The elements in a set are always unique, and they are stored in a specific order (ascending by default).

Console Output:

1 2 3

Inserting Elements

Inserting Elements:

Elements can be inserted into a set using the insert() function. If the element already exists, it will not be added again.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet;
    mySet.insert(5);
    mySet.insert(5); // Duplicate, won't be added
    mySet.insert(10);
    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Example Explanation:

Attempting to insert a duplicate value (5) does not change the set, as sets do not allow duplicate elements.

Console Output:

5 10

Removing Elements

Removing Elements:

Elements can be removed from a set using the erase() function. You can remove elements by value or by iterator.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4};
    mySet.erase(2); // Remove element by value
    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Example Explanation:

The element '2' is removed from the set. The remaining elements are printed in ascending order.

Console Output:

1 3 4

Checking Element Existence

Checking Element Existence:

To check if an element exists in a set, use the find() function. It returns an iterator to the element if found, otherwise it returns the end iterator.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4};
    if (mySet.find(3) != mySet.end()) {
        std::cout << "3 is in the set" << std::endl;
    } else {
        std::cout << "3 is not in the set" << std::endl;
    }
    return 0;
}
    

Example Explanation:

The find() method checks for the existence of the element '3' in the set. Since it is present, the output confirms its presence.

Console Output:

3 is in the set

Iterating Over a Set

Iterating Over a Set:

You can iterate over elements in a set using iterators, which provide a way to access each element sequentially.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {10, 20, 30, 40};
    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Example Explanation:

This example demonstrates how to iterate over a set using an iterator. Each element of the set is accessed in ascending order.

Console Output:

10 20 30 40

Set Size

Set Size:

The size() function returns the number of elements currently stored in the set.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4, 5};
    std::cout << "The size of the set is: " << mySet.size() << std::endl;
    return 0;
}
    

Example Explanation:

The size() method is used to determine the number of elements in the set, which is 5 in this example.

Console Output:

The size of the set is: 5

Clearing a Set

Clearing a Set:

The clear() function removes all elements from the set, leaving it empty.


#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4, 5};
    mySet.clear();
    std::cout << "The set is now empty with size: " << mySet.size() << std::endl;
    return 0;
}
    

Example Explanation:

After clearing the set, its size becomes 0, indicating that all elements have been removed.

Console Output:

The set is now empty with size: 0

Union of Sets

Union of Sets:

To perform a union of two sets, you can insert elements of one set into another. This combines both sets into one, preserving unique elements.


#include <iostream>
#include <set>

int main() {
    std::set<int> set1 = {1, 2, 3};
    std::set<int> set2 = {3, 4, 5};
    set1.insert(set2.begin(), set2.end());
    for (auto it = set1.begin(); it != set1.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Example Explanation:

This example demonstrates the union of two sets, resulting in a set containing all unique elements from both sets.

Console Output:

1 2 3 4 5

Intersection of Sets

Intersection of Sets:

To find the intersection of two sets, iterate through one set and check for each element's presence in the other set.


#include <iostream>
#include <set>

int main() {
    std::set<int> set1 = {1, 2, 3, 4};
    std::set<int> set2 = {3, 4, 5, 6};
    std::set<int> resultSet;
    for (auto it = set1.begin(); it != set1.end(); ++it) {
        if (set2.find(*it) != set2.end()) {
            resultSet.insert(*it);
        }
    }
    for (auto it = resultSet.begin(); it != resultSet.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Example Explanation:

The intersection operation results in a set containing elements common to both sets, which are '3' and '4'.

Console Output:

3 4

Difference of Sets

Difference of Sets:

The difference between two sets can be found by iterating through one set and inserting elements not found in the other set into a result set.


#include <iostream>
#include <set>

int main() {
    std::set<int> set1 = {1, 2, 3, 4};
    std::set<int> set2 = {3, 4, 5, 6};
    std::set<int> resultSet;
    for (auto it = set1.begin(); it != set1.end(); ++it) {
        if (set2.find(*it) == set2.end()) {
            resultSet.insert(*it);
        }
    }
    for (auto it = resultSet.begin(); it != resultSet.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
    

Example Explanation:

The difference operation results in a set containing elements from 'set1' that are not present in 'set2', which are '1' and '2'.

Console Output:

1 2

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025