techmore.in

C++ Sets

In C++, sets are associative containers that store unique elements following a specific order. The std::set is part of the Standard Template Library (STL) and is typically implemented as a balanced binary search tree (often a Red-Black Tree). Here’s a breakdown of how to use std::set:

Basic Operations with std::set

  1. Include the Necessary Header:

    cpp
    #include #include
  2. Declare a Set:

    cpp
    std::set<int> mySet;

    By default, std::set stores elements in ascending order. You can also specify a custom comparator to change the order.

  3. Add Elements:

    cpp
    mySet.insert(5); mySet.insert(10); mySet.insert(3);

    Note: std::set automatically ignores duplicate elements. If you try to insert an element that already exists, it will not be added again.

  4. Check for Presence of an Element:

    cpp
    if (mySet.find(10) != mySet.end()) { std::cout << "10 is in the set." << std::endl; }
  5. Remove Elements:

    cpp
    mySet.erase(5); // Removes the element with value 5
  6. Iterate Through the Set:

    cpp
    for (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl;
  7. Get the Size of the Set:

    cpp
    std::cout << "Size of the set: " << mySet.size() << std::endl;
  8. Check if the Set is Empty:

    cpp
    if (mySet.empty()) { std::cout << "Set is empty." << std::endl; }

Custom Comparator

If you want to store elements in a custom order, you can provide a comparator:

  1. Define a Custom Comparator:

    cpp
    struct Compare { bool operator()(int a, int b) const { return a > b; // Descending order } };
  2. Declare a Set with the Custom Comparator:

    cpp
    std::set<int, Compare> customSet;

Example Code

Here's a complete example illustrating basic operations and a custom comparator:

cpp
#include #include // Custom comparator for descending order struct Compare { bool operator()(int a, int b) const { return a > b; // Descending order } }; int main() { // Default set (ascending order) std::set<int> mySet = {5, 10, 3, 7, 7}; std::cout << "Default set:" << std::endl; for (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl; // Set with custom comparator (descending order) std::set<int, Compare> customSet = {5, 10, 3, 7}; std::cout << "Custom set (descending order):" << std::endl; for (const auto& elem : customSet) { std::cout << elem << " "; } std::cout << std::endl; // Check presence and remove elements if (mySet.find(10) != mySet.end()) { std::cout << "10 is in the default set." << std::endl; } mySet.erase(10); std::cout << "After removing 10, default set:" << std::endl; for (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl; return 0; }

Output

vbnet
Default set: 3 5 7 10 Custom set (descending order): 10 7 5 3 10 is in the default set. After removing 10, default set: 3 5 7

This example demonstrates the basic usage of std::set, including iteration, custom ordering, and element management.