C++ Sets

Welcome to The Coding College! In this tutorial, we’ll explore sets in C++, a part of the Standard Template Library (STL). Sets are a powerful and efficient data structure for managing unique elements in sorted order.

What Is a Set?

A set in C++ is an associative container that stores unique elements in a sorted manner. Unlike arrays or vectors, sets do not allow duplicate values and automatically arrange elements in ascending order.

Key Characteristics:

  • Unique Elements: Duplicate values are not stored.
  • Automatic Sorting: Elements are always sorted in ascending order by default.
  • Fast Lookup: Sets use a balanced binary search tree for efficient operations.

Declaring a Set

To use sets in C++, include the <set> header.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet; // Declare a set of integers

    return 0;
}

Common Operations

1. Insert Elements

Use the insert() method to add elements to a set. Duplicates are ignored.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet;

    mySet.insert(10);
    mySet.insert(20);
    mySet.insert(20); // Duplicate, won't be added
    mySet.insert(30);

    for (int x : mySet) {
        cout << x << " ";
    }

    return 0;
}

Output:

10 20 30  

2. Check Size

The size() method returns the number of elements in the set.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet = {10, 20, 30};

    cout << "Set size: " << mySet.size() << endl;

    return 0;
}

Output:

Set size: 3  

3. Erase Elements

You can remove elements using the erase() method.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet = {10, 20, 30};

    mySet.erase(20); // Remove element 20

    for (int x : mySet) {
        cout << x << " ";
    }

    return 0;
}

Output:

10 30  

4. Check for Element Existence

Use the find() method or the count() method to check if an element exists in the set.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet = {10, 20, 30};

    if (mySet.find(20) != mySet.end()) {
        cout << "20 is found in the set!" << endl;
    } else {
        cout << "20 is not in the set." << endl;
    }

    cout << "Count of 30: " << mySet.count(30) << endl;

    return 0;
}

Output:

20 is found in the set!  
Count of 30: 1  

5. Iterate Over a Set

You can traverse a set using range-based for loops or iterators.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet = {10, 20, 30};

    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        cout << *it << " ";
    }

    return 0;
}

Output:

10 20 30  

6. Clear a Set

The clear() method removes all elements from the set.

#include <set>
#include <iostream>
using namespace std;

int main() {
    set<int> mySet = {10, 20, 30};

    mySet.clear();

    cout << "Set size after clearing: " << mySet.size() << endl;

    return 0;
}

Output:

Set size after clearing: 0  

Example: Real-World Application

Remove Duplicates From a List

#include <set>
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 10, 30, 20, 40};
    set<int> uniqueNums(nums.begin(), nums.end());

    cout << "Unique elements: ";
    for (int x : uniqueNums) {
        cout << x << " ";
    }

    return 0;
}

Output:

Unique elements: 10 20 30 40  

Types of Sets

1. Set

Stores elements in ascending order (default behavior).

set<int> mySet;

2. Multiset

Allows duplicate elements.

multiset<int> myMultiSet;

3. Unordered Set

Stores elements without any specific order and uses hashing for faster lookups.

unordered_set<int> myUnorderedSet;

Summary

  • Advantages: Automatic sorting, unique elements, efficient lookups.
  • Key Methods: insert, erase, find, count, size, clear.
  • Applications: Removing duplicates, maintaining sorted data, and efficient lookups.

Learn More at The Coding College

For more tutorials on C++ and advanced data structures, visit The Coding College.

Leave a Comment