I am implementing the famous “subsets of a set” problem. I think I got a good working solution, but it contains duplicates. I was hoping that list.unique() would take of the situation, but since for a set the == operator isn’t defined, it doesn’t work. A set of sets doesn’t fix the situation either (using list of sets now).
Having 80% complete solution, I realize there is a better algorithm out there than one I came with. But I am wondering if there is a clever way to remove the duplicates without completely rewriting the algorithm?
Here’s my code:
MAIN.CPP:
#include "random.hpp"
using namespace std;
int main(void) {
subsets2();
getchar();
return 0;
}
Random.Cpp:
void getSubsets2(set<int> myset, list<set<int> > * ptr, int length) {
if (length == 1) {
ptr->push_back(myset);
}
else {
set<int> second(myset);
set<int>::iterator it;
ptr->push_back(myset);
it = myset.begin();
myset.erase(it);
it = second.begin();
++it;
second.erase(it);
getSubsets2(myset, ptr, length - 1);
getSubsets2(second, ptr, length - 1);
}
}
void subsets2(void) {
const int N = 4;
int myints[N] = {
88, 33, 23, 22
};
set<int> myset(myints, myints + N);
set<int> set2;
list<set<int> > mylist;
list<set<int> > * ptr;
ptr = & mylist;
list<set<int> > ::iterator it;
set<int>::iterator it2;
getSubsets2(myset, ptr, N);
mylist.unique();
for (it = mylist.begin(); it != mylist.end(); ++it) {
set2 = * it;
for (it2 = set2.begin(); it2 != set2.end(); ++it2) {
cout << * it2 << " ";
}
cout << "\n";
}
}
Output:
22 23 33 88
23 33 88
33 88
88
33
23 88
88
23
22 33 88
33 88
88
33
22 88
88
22
Unique() removes all
consecutiveduplicate elements from the container. So need to do sort mylist first before run unique().