How to Create a Set of Arrays in C++?
Last Updated :
21 Feb, 2024
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++
#include <array>
#include <iostream>
#include <set>
using namespace std;
int main()
{
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 };
mySet.insert(array1);
mySet.insert(array2);
mySet.insert(array3);
for ( auto & arr : mySet) {
for ( auto & element : arr) {
cout << element << ' ' ;
}
cout << endl;
}
}
|
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)
Share your thoughts in the comments
Please Login to comment...