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++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
int main()
{
set< int > s[ROW];
int num = 10;
for ( int i = 0; i < ROW; i++) {
for ( int j = 0; j < COL; j++) {
s[i].insert(num);
num += 5;
}
}
for ( int i = 0; i < ROW; i++) {
cout << "Elements at index " << i << ": " ;
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++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
int main()
{
set< int > s[ROW];
int num = 10;
for ( int i = 0; i < ROW; i++) {
for ( int j = 0; j < COL; j++) {
s[i].insert(num);
num += 5;
}
}
cout << "Before removal elements are:"
<< endl;
for ( int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": " ;
for ( auto x : s[i])
cout << x << " " ;
cout << endl;
}
s[2].erase(70);
s[1].erase(55);
cout << endl
<< "After removal elements are:"
<< endl;
for ( int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": " ;
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++
#include <bits/stdc++.h>
using namespace std;
#define ROW 2
int main()
{
set< int > s[ROW];
s[0].insert(10);
s[0].insert(15);
s[0].insert(35);
s[1].insert(20);
s[1].insert(30);
for ( int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": " ;
for ( auto it = s[i].begin();
it != s[i].end();
it++) {
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
Share your thoughts in the comments
Please Login to comment...