An array is described as a grouping of data elements arranged in a consecutive manner. Arrays hold various variables that share a common data type. This arrangement facilitates convenient access to the variables since they are stored in contiguous positions.
For example
This array consists of six elements. Assuming the array is named arr, the elements can be accessed via indices ranging from 0 to n-1, where n represents the array's size.
Arr[0] = 10
Arr[1] = 20
Arr[2] = 30
Arr[3] = 40
Arr[4] = 50
Arr[5] = 60
A set is a data structure that stores unique elements in an associative manner. In contrast to an array, the elements in a set cannot be altered once they are added. However, if there is a need to change a value in the set, it can be achieved by first removing the existing value and then adding the updated value.
Syntax
set<data_type> variable_name
Example
set<int> s ----- A set of integer values
establish<char> c ---- A collection of character values
Array of sets
When referring to an array of sets, we are talking about a two-dimensional array that consists of a predefined number of rows, where each row can contain elements of varying lengths.
In a collection of sets arranged in an array, each index in the array holds a distinct set that can be accessed using iterators.
Syntax
set<data_type> variable_name[size_of_array]
Example
A set<int> s[3] ----> An array of sets of integer type with a size of 3.
Insertion operation in the array of sets
Inserting data is accomplished by employing the insert method, where each row represents a distinct set in this scenario. Consequently, insertion is executed by utilizing an array that contains sets, as demonstrated by -
setvariable[rownumber].insert(element)
Example
s[0].insert(10) ----> Inserts 10 in row 1
s[1].insert(20) ----> Inserts 20 in row 2
#include <bits/stdc++.h>
using namespace std;
#define ROW 3
#define COL 4
int main()
{
set<int> s[ROW]; // Create an array of sets
int num = 10; // Initial element to be inserted
for (int i = 0; i < ROW; i++) { // iterate in row
// Insert the column elements
for (int j = 0; j < COL; j++) {
s[i].insert(num);
num += 10;
}
}
// Display the array of sets
for (int i = 0; i < ROW; i++) {
cout << "Row " << i + 1 << " = "
<< "Elements at index " << i << ": ";
// Print the array of sets
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
return 0;
}
Output
Row 1 = Elements at index 0: 10 20 30 40
Row 2 = Elements at index 1: 50 60 70 80
Row 3 = Elements at index 2: 90 100 110 120
Deletion operation in an array of sets
When discussing the deletion of an element in this context, we are referring to the action of eliminating the element from the set. The method used to remove an element from the set is called erase.
In this instance, we eliminated one item from collection 3 and another item from collection 2. Keep in mind, the collection's index is i-1 due to arrays starting at index 0.
#include <bits/stdc++.h>
using namespace std;
// Fixing rows and column
#define ROW 3
#define COL 4
int main()
{
set<int> s[ROW]; // Create array of set
int num = 10;
// Put elements in the set at a increment of 10
for (int i = 0; i < ROW; i++) {
// insert the column elements
for (int j = 0; j < COL; j++) {
s[i].insert(num);
num += 10;
}
}
cout << "Before removal elements are:"
<< endl;
// Print elements after insertion
for (int i = 0; i < ROW; i++) {
cout << "Elements at index "
<< i << ": ";
for (auto x : s[i])
cout << x << " ";
cout << endl;
}
s[2].erase(100); // Erase 100 from row 3 which means set 3
s[1].erase(50); // Erase 50 from row 2 which means set 2
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 20 30 40
Elements at index 1: 50 60 70 80
Elements at index 2: 90 100 110 120
After removal elements are:
Elements at index 0: 10 20 30 40
Elements at index 1: 60 70 80
Elements at index 2: 90 110 120
Traversal operation in an array of sets
When iterating through an array of sets, we cycle through each set and display all the elements within that set. Iterators play a crucial role in navigating through the elements of the set.
In this example, we are generating an array of sets with two rows. The first row contains a set with three elements, while the second row contains a set with two elements.
To iterate through an array of sets, we execute an external loop for the rows. Within the internal loop, we employ iterators to display each set.
#include <bits/stdc++.h>
using namespace std;
#define ROW 2 // Making a set of 2 rows
int main()
{
set<int> s[ROW]; // create an array of sets with 2 rows
// Insertion in row 1
s[0].insert(10); // insertion in column 1
s[0].insert(15); // insertion in column 2
s[0].insert(35); // insertion in column 3
// Insertion in row 2
s[1].insert(20); // insertion in column 1
s[1].insert(30); // insertion in column 2
// Traversal in array of sets
for (int i = 0; i < ROW; i++) { // Iterate in the row
cout << "Elements at index " << i << ": ";
// Now we have to Iterate in the set of a particular row
// So use an Iterate that runs from begin of set to end of that set
for (auto it = s[i].begin(); it != s[i].end(); it++) {
// Print the set value
cout << *it << ' ';
}
cout << endl;
}
return 0;
}
Output
Elements at index 0: 10 15 35
Elements at index 1: 20 30