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
Similar Reads
How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
2 min read
Vector of sets in C++ Prerequisite: Vectors in C++ STL Vectors are known as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container automatically. Sets are a type of associative containers in which each elem
7 min read
How to Create a Set of Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set
2 min read
How to Create a Stack of Set in C++? In C++ STL, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti
2 min read
How to Create a Deque of Sets in C++? In C++, a container called deque is a queue like container but allows for fast insertions and deletions at both ends. In this article, we will learn how to create a deque of sets in C++. Example: Input: mySet1 = {1, 4, 8, 9, 11} mySet2 = {1, 2, 3, 5, 7} Output: myDeque: [ {1, 4, 8, 9, 11}, {1, 2, 3,
2 min read
Sets of pairs in C++ Sets are a type of associative containers 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. Pair is a simple cont
3 min read
How to Create a Set of Pairs in C++? In C++, sets are associative containers that store unique elements. On the other hand, pairs allow the users to store two data of different or the same type into a single object. In this article, we will learn how we can create a set of pairs in C++. Example Input: p1={1, 2} p2 ={3, 4} Output: Eleme
2 min read
How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3
2 min read
How to Find the Size of a Set in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how we can find the size of a set container in C++. Example: Input: mySet={1
2 min read
How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in
2 min read