Signup/Sign In
LAST UPDATED: NOVEMBER 1, 2020

C++ STL Sets Program (Part 2)

Hello Everyone!

In this tutorial, we will learn about the working of a Set and its implementation in the C++ programming language. We will also discuss how to store the elements in descending order as well as how to copy the elements of one set into another without using an iterator.

What is a Set?

In programming, a Set is used to store unique values of a list and also automatically providing an ordering to its elements. By default, the ordering is in ascending order.

The elements are inserted using the insert() method. If the same value is inserted multiple times, the set automatically deletes the duplicates and only store the single copy of that element.

The elements of the Set are deleted using the erase() method.

For a better understanding of its implementation, refer to the well-commented C++ code given below.

Code:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//Function to print the elements of the set using an iterator
void show(set<int, greater<int>> s)
{
    //declaring an iterator to iterate through the set
    set<int>::iterator i;

    for (i = s.begin(); i != s.end(); i++)
    {
        cout << *i << "  "; //accessing the elements of the set using * as i stores the address to each element
    }

    cout << endl;
}

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the working of Sets (Part 2), in CPP  ===== \n\n\n\n";

    cout << " ***  Set automatically removes the duplicate elements and also sorts the input in ascending order (by default) *** \n\n";

    /* Set declaration (Set of integers)
        Note: By default, the declaration set<int> s stores the elements in ascending order
        But the below declaration stores them in decreasing order
    */
    set<int, greater<int>> s;

    //Filling the elements by using the insert() method.
    cout << "\n\nFilling the Set with integers in random order."; //Set automatically stores them in order

    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(64); //as 64 is added twice, but it will be stored only once in the set
    s.insert(82);
    s.insert(35);
    s.insert(54);

    cout << "\n\nThe number of elements in the Set are: " << s.size();

    cout << "\n\nThe elements of the Set s are: ";
    show(s);

    //Creating and initializing the set s2 with the elements of the set s
    set<int, greater<int>> s2(s.begin(), s.end());

    cout << "\n\nAfter copying the elements of the Set s into s2, the set s2 is : ";
    show(s2);

    cout << "\n\nThe number of elements in the Set s2 before using the erase() method are: " << s2.size();

    cout << "\n\nAfter deleting the element 35 from the Set s2 using the erase() method, it becomes: ";
    s2.erase(35);
    show(s2);

    cout << "\n\nThe number of elements in the Set s2 after using the erase() method are: " << s2.size();

    cout << "\n\n\n";

    return 0;
}

Output:

C++ Set Part 2

We hope that this post helped you develop a better understanding of the concept of Set and its implementation in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.