To check if a Set is empty in C++, you can use the empty method or compare the size to zero.
In this example,
#include <iostream>
#include <set>
int main() {
// Define set
std::set<int> mySet;
// Check if set is empty using empty method
if (mySet.empty()) {
std::cout << "Set is empty" << std::endl;
} else {
std::cout << "Set is not empty" << std::endl;
}
return 0;
}
Set is empty
In this example,
#include <iostream>
#include <set>
int main() {
// Define set
std::set<int> mySet;
// Check if set is empty using size method
if (mySet.size() == 0) {
std::cout << "Set is empty" << std::endl;
} else {
std::cout << "Set is not empty" << std::endl;
}
return 0;
}
Set is empty
In this tutorial, we learned How to check if a Set is Empty in C++ language with well detailed examples.