How to Create a Set of Arrays in C++?
Last Updated :
21 Feb, 2024
Improve
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 where each array is unique within the set. In other words, no two arrays in the set are identical. We can create the set of arrays by specifying the type of the set container to be array.
Syntax
set<array<ElementType, Size>> VariableName;
C++ Program to Create a Set of Arrays
// C++ program to Create a Set of Arrays
#include <array>
#include <iostream>
#include <set>
using namespace std;
// Driver Code
int main()
{
// Declare a set containing arrays of 3 integers
set<array<int, 3> > mySet;
array<int, 3> array1 = { 1, 2, 3 };
array<int, 3> array2 = { 4, 5, 6 };
array<int, 3> array3 = { 1, 2, 3 };
// Insert arrays into the set
mySet.insert(array1);
mySet.insert(array2);
mySet.insert(array3);
// Iterate over the set
for (auto& arr : mySet) {
// Iterate over each element of the array
for (auto& element : arr) {
// Output each element followed by a space
cout << element << ' ';
}
// Output a newline after each array
cout << endl;
}
}
Output
1 2 3 4 5 6
Time Complexity: O(M * N log N), where N is the number of arrays and M is the average number of elements in the arrays.
Auxiliary Space: O(N * M)