In C++, sets can be represented using arrays or the standard library's std::set
container. To create a set of size N with different types of items using arrays, you can use arrays with unique elements.
In this example,
stringSet
with initial string elements.#include <iostream>
#include <string>
int main() {
std::string stringSet[] = {"apple", "banana", "cherry"};
int size = sizeof(stringSet) / sizeof(stringSet[0]);
std::cout << "Size of stringSet: " << size << std::endl;
return 0;
}
Size of stringSet: N
In this example,
intSet
with initial integer elements.#include <iostream>
int main() {
int intSet[] = {1, 2, 3};
int size = sizeof(intSet) / sizeof(intSet[0]);
std::cout << "Size of intSet: " << size << std::endl;
return 0;
}
Size of intSet: N
In this tutorial, we learned How to create a Set of size N in C++ language with well detailed examples.