To create an empty set in C++, you can use the unordered_set container from the C++ Standard Template Library (STL).
In this example,
<unordered_set>
header to access the unordered_set container from the C++ STL.emptySet
, specifying the type of elements it will hold.#include <iostream>
#include <unordered_set>
int main() {
// Declare an empty set
std::unordered_set<int> emptySet;
// emptySet is now ready to be used as an empty set
// Example usage:
// Add elements to the set
emptySet.insert(5);
// Remove elements from the set
emptySet.erase(5);
// Check if an element is present in the set
bool containsElement = (emptySet.find(5) != emptySet.end());
std::cout << "Is element 5 present in the set: " << std::boolalpha << containsElement << std::endl;
return 0;
}
Is element 5 present in the set: false
In this tutorial, we learned How to create an Empty Set in C++ language with well detailed examples.