An array is defined as the collection of data items stored in a contiguous manner. An array stores different variables of the same type. It becomes easier to access the variables as they are stored at continuous locations.
For example
This is an array that contains six elements. Let's say the array name is arr. Now, if we want to access the elements, they can access the indices 0 to n-1. Here, n is the size of the array.
Arr[0] = 10
Arr[1] = 20
Arr[2] = 30
Arr[3] = 40
Arr[4] = 50
Arr[5] = 60
A set is an associative container in which the elements are unique. Unlike an array, the value entered in a set cannot be modified after adding it. However, if we want to update the value in the set, we can remove it first and then enter the modified value.
Syntax
set<data_type> variable_name
Example
set<int> s ----- A set of integer values
set<char> c ---- A set of character values
Array of sets
When we say an array of sets, it is a two-dimensional array with a fixed number of rows. Each row can have variable lengths.
In an array of sets, each array index stores a set. The set is accessible via iterators. \
Syntax
set<data_type> variable_name[size_of_array]
Example
set<int> s[3] ----> A array of set of int type with size 3
Insertion operation in the array of sets
Insertion will happen with the insert function as each row is a set here. So, we insert using an array of sets using -
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 we say deletion of an element here, we are removing the element from the set. The function to remove an element from the set is erase.
In this example, we have removed one element from set 3 and one element from set 2. Remember, the set index is i-1 as the array index starts at 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 we traverse an array of sets, we iterate through each set and print all the elements in that set. Iterators are used to traverse the set elements.
In the below example, we create an array of sets having two rows. Row one has three elements in the set, and row 2 has two elements in the set.
To traverse an array of sets, we run an outer loop for the rows. In the inner loop, we use iterators to print 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