Open In App

Array of Sets in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element.

Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.

Therefore, an array of sets is a two-dimensional array with a fixed number of rows where each row is a set of variable lengths. Each index of array stores a set that can be traversed and accessed using iterators.

Syntax:

set <data_type> S[size];

Example:

set< int >S[5], where S is the array of sets of int of size 5

Insertion in the array of sets: The insertion of elements in each set is done using the insert() function. Below is the example to demonstrate the insertion operation in an array of sets:

C++




// C++ program to demonstrate the
// insertion in the array of sets
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
  
// Driver Code
int main()
{
    // Declaring array of sets
    set<int> s[ROW];
  
    // Elements to insert
    // in set
    int num = 10;
  
    // Inserting elements
    // into sets
    for (int i = 0; i < ROW; i++) {
        // Insert the column elements
        for (int j = 0; j < COL; j++) {
            s[i].insert(num);
            num += 5;
        }
    }
  
    // Display the array of sets
    for (int i = 0; i < ROW; i++) {
        cout << "Elements at index " << i << ": ";
  
        // Print the array of sets
        for (auto x : s[i])
            cout << x << " ";
  
        cout << endl;
    }
  
    return 0;
}


Output:

Elements at index 0: 10 15 20 25 30 
Elements at index 1: 35 40 45 50 55 
Elements at index 2: 60 65 70 75 80 
Elements at index 3: 85 90 95 100 105

Deletion of an element in the array of sets: The deletion of elements from each set is done using erase() function. Below is the example to demonstrate the deletion operation in the array of sets:

C++




// C++ program to demonstrate the
// deletion in the array of sets
#include <bits/stdc++.h>
using namespace std;
  
// Defining the length of array
// and number of elements in
// each set
#define ROW 4
#define COL 5
  
// Driver Code
int main()
{
    // Declaring array of sets
    set<int> s[ROW];
    int num = 10;
  
    // Inserting elements in the set
    // at each index of array
    for (int i = 0; i < ROW; i++) {
  
        // insert the column elements
        for (int j = 0; j < COL; j++) {
            s[i].insert(num);
            num += 5;
        }
    }
  
    cout << "Before removal elements are:"
         << endl;
  
    // Display the array of sets
    for (int i = 0; i < ROW; i++) {
        cout << "Elements at index "
             << i << ": ";
        for (auto x : s[i])
            cout << x << " ";
        cout << endl;
    }
  
    // Erase 70 from 3rd set
    s[2].erase(70);
  
    // Erase 55 from 2nd set
    s[1].erase(55);
  
    // Display the array of sets
    // after removal of elements
    cout << endl
         << "After removal elements are:"
         << endl;
  
    for (int i = 0; i < ROW; i++) {
        cout << "Elements at index "
             << i << ": ";
  
        // Print the current set
        for (auto x : s[i])
            cout << x << " ";
  
        cout << endl;
    }
  
    return 0;
}


Output:

Before removal elements are:
Elements at index 0: 10 15 20 25 30 
Elements at index 1: 35 40 45 50 55 
Elements at index 2: 60 65 70 75 80 
Elements at index 3: 85 90 95 100 105 

After removal elements are:
Elements at index 0: 10 15 20 25 30 
Elements at index 1: 35 40 45 50 
Elements at index 2: 60 65 75 80 
Elements at index 3: 85 90 95 100 105

Traversal in the array of sets: Each set is traversed from an array of sets with the help of array[index] and traversal in sets is perform using iterators. Below is the program to illustrate the traversal in the array of sets:

C++




// C++ program to demonstrate the
// traversal in the array of sets
#include <bits/stdc++.h>
using namespace std;
#define ROW 2
  
// Driver Code
int main()
{
    // Declaring array of sets
    set<int> s[ROW];
  
    // Inserting elements into sets
    // Insert 10, 15, and 35 in 1st set
    s[0].insert(10);
    s[0].insert(15);
    s[0].insert(35);
  
    // Insert 20 and 30 in 2nd set
    s[1].insert(20);
    s[1].insert(30);
  
    // Traversing of sets s to print
    // elements stored in it
    for (int i = 0; i < ROW; i++) {
        cout << "Elements at index "
             << i << ": ";
  
        // Traversing and printing
        // element  at each column,
        // begin() is the starting
        // iterator, end() is the
        // ending iterator
        for (auto it = s[i].begin();
             it != s[i].end();
             it++) {
  
            // (*it) is used to get the
            // value at iterator is pointing
            cout << *it << ' ';
        }
  
        cout << endl;
    }
  
    return 0;
}


Output:

Elements at index 0: 10 15 35 
Elements at index 1: 20 30


Last Updated : 04 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads