SET in C++ STL

SET in C++ STL

SET DESCRIPTION:

  • A container that represents a sorted set of unique elements.

  • Ensures that the elements are stored in sorted order according to the provided comparison criterion.

  • Suitable for scenarios where a collection of unique, sorted elements is required.


ALL THE POSSIBLE WAY TO DECLARE, INITIALIZE AND PRINTING SET:

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
    // Declaration of an empty set of integers
    set<int> mySet;

    // Declaration of a set of doubles
    set<double> doubleSet;

    // Declaration of a set of strings
    set<string> stringSet;

    // Initializing set with values using initializer list
    set<int> initializedSet = {1, 2, 3, 4, 5};

    // Dynamic initialization using insert
    set<int> dynamicSet;
    dynamicSet.insert(10);
    dynamicSet.insert(20);
    dynamicSet.insert(30);

    // Copying from another set
    set<int> sourceSet = {1, 2, 3, 4, 5};
    set<int> copiedSet(sourceSet); // Copy constructor

    // Printing set using for loop and iterators
    cout << "Initialized Set (iterator): ";
    for (set<int>::iterator it = initializedSet.begin(); it != initializedSet.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // Printing set using for loop and auto keyword
    cout << "Dynamic Set (auto): ";
    for (auto element : dynamicSet) {
        cout << element << " ";
    }
    cout << endl;

    // Printing set using range-based for loop (corrected)
    cout << "Copied Set (range-based for loop): ";
    for (auto it = copiedSet.begin(); it != copiedSet.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

OUTPUT:

Initialized Set (iterator): 1 2 3 4 5 
Dynamic Set (auto): 10 20 30 
Copied Set (range-based for loop): 1 2 3 4 5

SET MEMBER FUNCTIONS:

  1. empty():

    • Works: Returns whether the set is empty.

    • Syntax: mySet.empty()

    • Example:

        set<int> mySet = {1, 2, 3};
        bool isEmpty = mySet.empty();
      
  2. size():

    • Works: Returns the number of elements in the set.

    • Syntax: mySet.size()

Example:

set<int> mySet = {1, 2, 3};
size_t setSize = mySet.size();

CODE:

#include <iostream>
#include <set>

using namespace std;

int main() {
    set<int> mySet = {1, 2, 3};

    // Check if the set is empty
    bool isEmpty = mySet.empty();
    cout << "Is the set empty? " << (isEmpty ? "Yes" : "No") << endl;

    // Get the number of elements in the set
    size_t setSize = mySet.size();
    cout << "Number of elements in the set: " << setSize << endl;

    return 0;
}

OUTPUT:

Is the set empty? No
Number of elements in the set: 3

SET MODIFIERS:

  1. insert(const T& val):

    • Works: Inserts an element into the set.

    • Syntax: mySet.insert(value)

    • Example:

        set<int> mySet = {1, 2, 3};
        mySet.insert(4);
      
  2. erase(const T& val):

    • Works: Removes an element from the set.

    • Syntax: mySet.erase(value)

    • Example:

        set<int> mySet = {1, 2, 3};
        mySet.erase(2);
      
  3. clear():

    • Works: Removes all elements from the set.

    • Syntax: mySet.clear()

Example:

set<int> mySet = {1, 2, 3};
mySet.clear();

CODE:

#include <iostream>
#include <set>

using namespace std;

int main() {
    // Example for insert()
    set<int> mySet = {1, 2, 3};
    mySet.insert(4);

    // Display elements after insert()
    cout << "Set after insert: ";
    for (const auto& elem : mySet) {
        cout << elem << " ";
    }
    cout << endl;

    // Example for erase()
    mySet.erase(2);

    // Display elements after erase()
    cout << "Set after erase: ";
    for (const auto& elem : mySet) {
        cout << elem << " ";
    }
    cout << endl;

    // Example for clear()
    mySet.clear();

    // Display elements after clear()
    cout << "Set after clear: ";
    for (const auto& elem : mySet) {
        cout << elem << " ";
    }
    cout << endl;

    return 0;
}

OUTPUT:

Set after insert: 1 2 3 4 
Set after erase: 1 3 4 
Set after clear:

SET ALGORITHM

  1. find(const T& val):

    • Works: Returns an iterator to the element with the specified value or end() if not found.

    • Syntax: mySet.find(value)

    • Example:

        set<int> mySet = {1, 2, 3};
        auto it = mySet.find(2);
      
  2. count(const T& val):

    • Works: Returns the number of elements with the specified value (0 or 1 for set).

    • Syntax: mySet.count(value)

    • Example:

        set<int> mySet = {1, 2, 3};
        int count = mySet.count(2);
      
  3. lower_bound(const T& val):

    • Works: Returns an iterator pointing to the first element not less than the specified value.

    • Syntax: mySet.lower_bound(value)

    • Example:

        set<int> mySet = {1, 2, 3};
        auto it = mySet.lower_bound(2);
      
  4. upper_bound(const T& val):

    • Works: Returns an iterator pointing to the first element greater than the specified value.

    • Syntax: mySet.upper_bound(value)

    • Example:

        set<int> mySet = {1, 2, 3};
        auto it = mySet.upper_bound(2);
      
  5. equal_range(const T& val):

    • Works: Returns a pair of iterators representing the range of elements with the specified value.

    • Syntax: mySet.equal_range(value)

Example:

set<int> mySet = {1, 2, 3};
auto range = mySet.equal_range(2);

CODE:

#include <iostream>
#include <set>

using namespace std;

int main() {
    // Example for find()
    set<int> mySet = {1, 2, 3};
    auto itFind = mySet.find(2);
    if (itFind != mySet.end()) {
        cout << "Element 2 found in the set." << endl;
    } else {
        cout << "Element 2 not found in the set." << endl;
    }

    // Example for count()
    int count = mySet.count(2);
    cout << "Number of occurrences of 2 in the set: " << count << endl;

    // Example for lower_bound()
    auto itLowerBound = mySet.lower_bound(2);
    cout << "Lower bound of 2 in the set: " << *itLowerBound << endl;

    // Example for upper_bound()
    auto itUpperBound = mySet.upper_bound(2);
    cout << "Upper bound of 2 in the set: " << *itUpperBound << endl;

    // Example for equal_range()
    auto range = mySet.equal_range(2);
    cout << "Equal range for 2 in the set: [" << *range.first << ", " << *range.second << ")" << endl;

    return 0;
}

OUTPUT:

Element 2 found in the set.
Number of occurrences of 2 in the set: 1
Lower bound of 2 in the set: 2
Upper bound of 2 in the set: 3
Equal range for 2 in the set: [2, 3)

MULTISET DESCRIPTION:

  • A container that represents a sorted set of elements where multiple elements with the same value are allowed.

  • Ensures that the elements are stored in sorted order according to the provided comparison criterion.

  • Allows duplicate elements.

  • Suitable for scenarios where a collection of sorted elements with duplicates is required.


ALL THE POSSIBLE WAY TO DECLARE, INITIALIZE AND PRINTING MULTISET:

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
    // Declaration of an empty multiset of integers
    multiset<int> myMultiset;

    // Declaration of a multiset of doubles
    multiset<double> doubleMultiset;

    // Declaration of a multiset of strings
    multiset<string> stringMultiset;

    // Initializing multiset with values using initializer list
    multiset<int> initializedMultiset = {1, 2, 3, 4, 5};

    // Dynamic initialization using insert
    multiset<int> dynamicMultiset;
    dynamicMultiset.insert(10);
    dynamicMultiset.insert(20);
    dynamicMultiset.insert(30);

    // Copying from another multiset
    multiset<int> sourceMultiset = {1, 2, 3, 4, 5};
    multiset<int> copiedMultiset(sourceMultiset); // Copy constructor

    // Printing multiset using for loop and iterators
    cout << "Initialized MultiSet (iterator): ";
    for (multiset<int>::iterator it = initializedMultiset.begin(); it != initializedMultiset.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // Printing multiset using for loop and auto keyword
    cout << "Dynamic MultiSet (auto): ";
    for (auto element : dynamicMultiset) {
        cout << element << " ";
    }
    cout << endl;

    // Printing multiset using range-based for loop
    cout << "Copied MultiSet (range-based for loop): ";
    for (auto it = copiedMultiset.begin(); it != copiedMultiset.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

OUTPUT:

Initialized MultiSet (iterator): 1 2 3 4 5 
Dynamic MultiSet (auto): 10 20 30 
Copied MultiSet (range-based for loop): 1 2 3 4 5

MULTISET MEMBER FUNCTIONS:

  1. empty():

    • Works: Returns whether the multiset is empty.

    • Syntax: myMultiset.empty()

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        bool isEmpty = myMultiset.empty();
      
  2. size():

    • Works: Returns the number of elements in the multiset.

    • Syntax: myMultiset.size()

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        size_t multisetSize = myMultiset.size();
      

      CODE:

        #include <iostream>
        #include <set>
      
        using namespace std;
      
        int main() {
            // Example for empty()
            multiset<int> myMultiset = {1, 2, 2, 3};
            bool isEmpty = myMultiset.empty();
            cout << "Is the multiset empty? " << (isEmpty ? "Yes" : "No") << endl;
      
            // Example for size()
            size_t multisetSize = myMultiset.size();
            cout << "Number of elements in the multiset: " << multisetSize << endl;
      
            return 0;
        }
      

      OUTPUT:

        Is the multiset empty? No
        Number of elements in the multiset: 4
      

MULTISET MODIFIERS:

  1. insert(const T& val):

    • Works: Inserts an element into the multiset.

    • Syntax: myMultiset.insert(value)

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        myMultiset.insert(4);
      
  2. erase(const T& val):

    • Works: Removes an element from the multiset.

    • Syntax: myMultiset.erase(value)

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        myMultiset.erase(2);
      
  3. clear():

    • Works: Removes all elements from the multiset.

    • Syntax: myMultiset.clear()

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        myMultiset.clear();
      

      CODE:

        #include <iostream>
        #include <set>
      
        using namespace std;
      
        int main() {
            // Example for insert()
            multiset<int> myMultiset = {1, 2, 2, 3};
            myMultiset.insert(4);
      
            // Display elements after insert()
            cout << "Multiset after insert: ";
            for (const auto& elem : myMultiset) {
                cout << elem << " ";
            }
            cout << endl;
      
            // Example for erase()
            myMultiset.erase(2);
      
            // Display elements after erase()
            cout << "Multiset after erase: ";
            for (const auto& elem : myMultiset) {
                cout << elem << " ";
            }
            cout << endl;
      
            // Example for clear()
            myMultiset.clear();
      
            // Display elements after clear()
            cout << "Multiset after clear: ";
            for (const auto& elem : myMultiset) {
                cout << elem << " ";
            }
            cout << endl;
      
            return 0;
        }
      

      OUTPUT:

        Multiset after insert: 1 2 2 3 4 
        Multiset after erase: 1 3 4 
        Multiset after clear:
      

MULTISET ALGORITHMS:

  1. find(const T& val):

    • Works: Returns an iterator to an element with the specified value or end() if not found.

    • Syntax: myMultiset.find(value)

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        auto it = myMultiset.find(2);
      
  2. count(const T& val):

    • Works: Returns the number of elements with the specified value.

    • Syntax: myMultiset.count(value)

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        int count = myMultiset.count(2);
      
  3. lower_bound(const T& val):

    • Works: Returns an iterator pointing to the first element not less than the specified value.

    • Syntax: myMultiset.lower_bound(value)

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        auto it = myMultiset.lower_bound(2);
      
  4. upper_bound(const T& val):

    • Works: Returns an iterator pointing to the first element greater than the specified value.

    • Syntax: myMultiset.upper_bound(value)

    • Example:

        multiset<int> myMultiset = {1, 2, 2, 3};
        auto it = myMultiset.upper_bound(2);
      

5.equal_range(const T& val):

  • Works: Returns a pair of iterators representing the range of elements with the specified value.

  • Syntax: myMultiset.equal_range(value)

  • Example:

      multiset<int> myMultiset = {1, 2, 2, 3};
      auto range = myMultiset.equal_range(2);
    

    CODE:

      #include <iostream>
      #include <set>
    
      using namespace std;
    
      int main() {
          // Example for find()
          multiset<int> myMultiset = {1, 2, 2, 3};
          auto itFind = myMultiset.find(2);
          if (itFind != myMultiset.end()) {
              cout << "Element 2 found in the multiset." << endl;
          } else {
              cout << "Element 2 not found in the multiset." << endl;
          }
    
          // Example for count()
          int count = myMultiset.count(2);
          cout << "Number of occurrences of 2 in the multiset: " << count << endl;
    
          // Example for lower_bound()
          auto itLowerBound = myMultiset.lower_bound(2);
          cout << "Lower bound of 2 in the multiset: " << *itLowerBound << endl;
    
          // Example for upper_bound()
          auto itUpperBound = myMultiset.upper_bound(2);
          cout << "Upper bound of 2 in the multiset: " << *itUpperBound << endl;
    
          // Example for equal_range()
          auto range = myMultiset.equal_range(2);
          cout << "Equal range for 2 in the multiset: [" << *range.first << ", " << *range.second << ")" << endl;
    
          return 0;
      }
    

    OUTPUT:

      Element 2 found in the multiset.
      Number of occurrences of 2 in the multiset: 2
      Lower bound of 2 in the multiset: 2
      Upper bound of 2 in the multiset: 3
      Equal range for 2 in the multiset: [2, 3)
    

UNORDERED SET DESCRIPTION:

  • A container that represents an unordered set of unique elements.

  • Stores elements in an unordered manner, providing faster average access times than ordered containers.

  • Suitable for scenarios where uniqueness is important, and the order of elements does not matter.


ALL THE POSSIBLE WAY TO DECLARE, INITIALIZE AND PRINTING UNORDERED SET:

#include <iostream>
#include <unordered_set>
#include <string>

using namespace std;

int main() {
    // Declaration of an empty unordered_set of integers
    unordered_set<int> myUnorderedSet;

    // Declaration of an unordered_set of doubles
    unordered_set<double> doubleUnorderedSet;

    // Declaration of an unordered_set of strings
    unordered_set<string> stringUnorderedSet;

    // Initializing unordered_set with values using initializer list
    unordered_set<int> initializedUnorderedSet = {1, 2, 3, 4, 5};

    // Dynamic initialization using insert
    unordered_set<int> dynamicUnorderedSet;
    dynamicUnorderedSet.insert(10);
    dynamicUnorderedSet.insert(20);
    dynamicUnorderedSet.insert(30);

    // Printing unordered_set using range-based for loop
    cout << "Initialized UnorderedSet (range-based for loop): ";
    for (const auto& element : initializedUnorderedSet) {
        cout << element << " ";
    }
    cout << endl;

    // Printing unordered_set using for loop and iterators
    cout << "Dynamic UnorderedSet (iterator): ";
    for (unordered_set<int>::iterator it = dynamicUnorderedSet.begin(); it != dynamicUnorderedSet.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    // Printing unordered_set using for loop and auto keyword
    cout << "Initialized UnorderedSet (auto): ";
    for (auto element : initializedUnorderedSet) {
        cout << element << " ";
    }
    cout << endl;

    return 0;
}

OUTPUT:

Initialized UnorderedSet (range-based for loop): 5 1 2 3 4
Dynamic UnorderedSet (iterator): 30 20 10
Initialized UnorderedSet (auto): 5 1 2 3 4

UNORDERED SET MEMBER FUNCTIONS:

  1. empty():

    • Works: Returns whether the unordered set is empty.

    • Syntax: myUnorderedSet.empty()

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        bool isEmpty = myUnorderedSet.empty();
      
  2. size():

    • Works: Returns the number of elements in the unordered set.

    • Syntax: myUnorderedSet.size()

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        size_t setSize = myUnorderedSet.size();
      

      CODE:

        #include <iostream>
        #include <unordered_set>
      
        using namespace std;
      
        int main() {
            // Example for empty()
            unordered_set<int> myUnorderedSet = {1, 2, 3};
            bool isEmpty = myUnorderedSet.empty();
            cout << "Is the unordered set empty? " << (isEmpty ? "Yes" : "No") << endl;
      
            // Example for size()
            size_t setSize = myUnorderedSet.size();
            cout << "Number of elements in the unordered set: " << setSize << endl;
      
            return 0;
        }
      

      OUTPUT:

        Is the unordered set empty? No
        Number of elements in the unordered set: 3
      

UNORDERED SET MODIFIERS:

  1. insert(const T& val):

    • Works: Inserts an element into the unordered set.

    • Syntax: myUnorderedSet.insert(value)

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        myUnorderedSet.insert(4);
      
  2. erase(const T& val):

    • Works: Removes an element from the unordered set.

    • Syntax: myUnorderedSet.erase(value)

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        myUnorderedSet.erase(2);
      
  3. clear():

    • Works: Removes all elements from the unordered set.

    • Syntax: myUnorderedSet.clear()

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        myUnorderedSet.clear();
      

      CODE:

        #include <iostream>
        #include <unordered_set>
      
        using namespace std;
      
        int main() {
            // Example for insert()
            unordered_set<int> myUnorderedSet = {1, 2, 3};
            myUnorderedSet.insert(4);
      
            // Display elements after insert()
            cout << "Unordered set after insert: ";
            for (const auto& elem : myUnorderedSet) {
                cout << elem << " ";
            }
            cout << endl;
      
            // Example for erase()
            myUnorderedSet.erase(2);
      
            // Display elements after erase()
            cout << "Unordered set after erase: ";
            for (const auto& elem : myUnorderedSet) {
                cout << elem << " ";
            }
            cout << endl;
      
            // Example for clear()
            myUnorderedSet.clear();
      
            // Display elements after clear()
            cout << "Unordered set after clear: ";
            for (const auto& elem : myUnorderedSet) {
                cout << elem << " ";
            }
            cout << endl;
      
            return 0;
        }
      

      OUTPUT:

        Unordered set after insert: 1 2 3 4 
        Unordered set after erase: 1 3 4 
        Unordered set after clear:
      

UNORDERED SET ALGORITHM:

  1. find(const T& val):

    • Works: Returns an iterator to an element with the specified value or end() if not found.

    • Syntax: myUnorderedSet.find(value)

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        auto it = myUnorderedSet.find(2);
      
  2. count(const T& val):

    • Works: Returns the number of elements with the specified value (0 or 1 for unordered_set).

    • Syntax: myUnorderedSet.count(value)

    • Example:

        unordered_set<int> myUnorderedSet = {1, 2, 3};
        size_t count = myUnorderedSet.count(2);
      

      CODE:

        #include <iostream>
        #include <unordered_set>
      
        using namespace std;
      
        int main() {
            // Example for find()
            unordered_set<int> myUnorderedSet = {1, 2, 3};
            auto itFind = myUnorderedSet.find(2);
            if (itFind != myUnorderedSet.end()) {
                cout << "Element 2 found in the unordered set." << endl;
            } else {
                cout << "Element 2 not found in the unordered set." << endl;
            }
      
            // Example for count()
            size_t count = myUnorderedSet.count(2);
            cout << "Number of occurrences of 2 in the unordered set: " << count << endl;
      
            return 0;
        }
      

      OUTPUT:

        Element 2 found in the unordered set.
        Number of occurrences of 2 in the unordered set: 1
      

CONGRATULATIONS ! NOW YOU ARE THE MASTER OF SET CONTAINER.

Did you find this article valuable?

Support AL NAFIS FUAD SHUVO by becoming a sponsor. Any amount is appreciated!