In C++, the set container is designed to store a collection of unique, sorted elements. An array in C++ is a collection of items stored at contiguous memory locations. Combining these two data structures, we can create a set of arrays, which ensures that each array within the set is unique. This means no two arrays in the set are identical, and the arrays are stored in a sorted order based on their values.
Set of Arrays in C++
A set of arrays in C++ refers to a collection where each array is unique within the set. This is achieved by specifying the type of the set container to be an array. When we use a set, it automatically takes care of the uniqueness and sorting of its elements.
Syntax
The syntax to declare a set of arrays is as follows:
#include <iostream>
#include <set>
#include <array>
int main() {
std::set<std::array<int, 3>> setOfArrays;
// Example usage
return 0;
}
Detailed Explanation
- Include Necessary Headers:
#include <iostream>: For input and output operations.#include <set>: For using thesetcontainer.#include <array>: For using thearraycontainer.
- Declare the Set of Arrays:
std::set<std::array<int, 3>> setOfArrays;std::set: Thesetcontainer from the Standard Template Library (STL) that ensures all elements are unique and sorted.std::array<int, 3>: An array of integers with a fixed size of 3. The type of elements stored in the set is specified as this array type.
Example Code
Here’s an example that demonstrates how to use a set of arrays in C++:
#include <iostream>
#include <set>
#include <array>
// Function to print the elements of the set
void printSet(const std::set<std::array<int, 3>>& setOfArrays) {
for (const auto& arr : setOfArrays) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}
}
int main() {
// Declare a set of arrays
std::set<std::array<int, 3>> setOfArrays;
// Insert arrays into the set
setOfArrays.insert({{1, 2, 3}});
setOfArrays.insert({{4, 5, 6}});
setOfArrays.insert({{1, 2, 3}}); // Duplicate array, will not be added
setOfArrays.insert({{7, 8, 9}});
// Print the set
printSet(setOfArrays);
return 0;
}
Explanation of above Code
- Function to Print Set Elements:
printSetfunction takes asetof arrays and prints each array.- The function iterates through each array in the set and prints its elements.
- Declare and Initialize Set:
std::set<std::array<int, 3>> setOfArrays;- A set of arrays is declared to store arrays of three integers.
- Insert Arrays into the Set:
setOfArrays.insert({{1, 2, 3}});: Inserts an array{1, 2, 3}into the set.setOfArrays.insert({{4, 5, 6}});: Inserts another array{4, 5, 6}into the set.setOfArrays.insert({{1, 2, 3}});: Attempts to insert a duplicate array{1, 2, 3}, which will not be added because sets do not allow duplicate elements.setOfArrays.insert({{7, 8, 9}});: Inserts another array{7, 8, 9}into the set.
- Print the Set:
- The
printSetfunction is called to display the contents of the set. - The elements will be printed in sorted order, demonstrating the unique and ordered nature of the set.
- The
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)
Inserting and Printing Arrays
Let’s take another example where we insert multiple arrays into a set and print them:
#include <iostream>
#include <set>
#include <array>
// Function to print the elements of the set
void printSet(const std::set<std::array<int, 3>>& setOfArrays) {
for (const auto& arr : setOfArrays) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}
}
int main() {
// Declare a set of arrays
std::set<std::array<int, 3>> setOfArrays;
// Insert arrays into the set
setOfArrays.insert({{3, 1, 2}});
setOfArrays.insert({{4, 5, 6}});
setOfArrays.insert({{1, 2, 3}});
setOfArrays.insert({{2, 2, 2}});
setOfArrays.insert({{1, 1, 1}});
// Print the set
std::cout << "Set contents after insertion:" << std::endl;
printSet(setOfArrays);
return 0;
}
Output:
Set contents after insertion:
1 1 1
1 2 3
2 2 2
3 1 2
4 5 6
Explanation:
- Function to Print Set Elements:
- The
printSetfunction iterates through each array in the set and prints its elements.
- The
- Declare and Initialize Set:
std::set<std::array<int, 3>> setOfArrays;declares a set to store arrays of three integers.
- Insert Arrays into the Set:
- Various arrays are inserted into the set. Note that they are not inserted in sorted order.
- The set automatically sorts and stores the arrays uniquely.
- Print the Set:
- The set is printed, showing the elements in sorted order.
Removing Arrays from the Set
#include <iostream>
#include <set>
#include <array>
// Function to print the elements of the set
void printSet(const std::set<std::array<int, 3>>& setOfArrays) {
for (const auto& arr : setOfArrays) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}
}
int main() {
// Declare a set of arrays
std::set<std::array<int, 3>> setOfArrays;
// Insert arrays into the set
setOfArrays.insert({{3, 1, 2}});
setOfArrays.insert({{4, 5, 6}});
setOfArrays.insert({{1, 2, 3}});
setOfArrays.insert({{2, 2, 2}});
setOfArrays.insert({{1, 1, 1}});
// Remove an array from the set
setOfArrays.erase({{1, 2, 3}});
// Print the set after removal
std::cout << "Set contents after removal:" << std::endl;
printSet(setOfArrays);
return 0;
}
Explanation:
- Insert Arrays:
- Arrays are inserted into the set as in the previous example.
- Remove an Array:
setOfArrays.erase({{1, 2, 3}});removes the array{1, 2, 3}from the set.
- Print the Set:
- The set is printed after the removal to show the updated contents.
Output:
Set contents after removal:
1 1 1
2 2 2
3 1 2
4 5 6
Finding Elements in the Set
This example shows how to find specific arrays in the set:
#include <iostream>
#include <set>
#include <array>
int main() {
// Declare a set of arrays
std::set<std::array<int, 3>> setOfArrays;
// Insert arrays into the set
setOfArrays.insert({{3, 1, 2}});
setOfArrays.insert({{4, 5, 6}});
setOfArrays.insert({{1, 2, 3}});
setOfArrays.insert({{2, 2, 2}});
setOfArrays.insert({{1, 1, 1}});
// Check if specific arrays exist in the set
std::array<int, 3> searchArray1 = {1, 2, 3};
std::array<int, 3> searchArray2 = {2, 3, 4};
if (setOfArrays.find(searchArray1) != setOfArrays.end()) {
std::cout << "Array {1, 2, 3} found in the set." << std::endl;
} else {
std::cout << "Array {1, 2, 3} not found in the set." << std::endl;
}
if (setOfArrays.find(searchArray2) != setOfArrays.end()) {
std::cout << "Array {2, 3, 4} found in the set." << std::endl;
} else {
std::cout << "Array {2, 3, 4} not found in the set." << std::endl;
}
return 0;
}
Explanation:
- Insert Arrays:
- Arrays are inserted into the set as in the previous examples.
- Search for Arrays:
- The code searches for two arrays:
{1, 2, 3}and{2, 3, 4}. setOfArrays.find(searchArray1) != setOfArrays.end()checks if{1, 2, 3}is in the set.setOfArrays.find(searchArray2) != setOfArrays.end()checks if{2, 3, 4}is in the set.
- The code searches for two arrays:
Output:
Array {1, 2, 3} found in the set.
Array {2, 3, 4} not found in the set.
Conclusion
By using a set of arrays, we ensure that each array is unique and the elements are automatically sorted. This combination is powerful for managing collections of fixed-size arrays where uniqueness and order are important. The set container handles all the complexities of maintaining these properties, making it a convenient and efficient choice for such tasks in C++.





Leave a Reply