简体   繁体   中英

Removing Duplicates from a list of sets

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 consecutive duplicate elements from the container. So need to do sort mylist first before run unique().

   mylist.sort();
   mylist.unique();

Just as another way of doing this, std::less<T> is defined for all standard containers. Hence, we can define something like:

std::set<std::set<int>, std::less<std::set<int>>> set_of_sets;

This will automatically filter out duplicate sets. A full example:

#include <set>
#include <vector>
#include <iostream>
#include <functional>

int main()
{
    std::vector<std::vector<int>> x = {{1,2,3}, {1,2}, {1,2,3}, {4,5,6},
                                       {4,5}, {5,6}, {4,5,6}};
    std::set<std::set<int>, std::less<std::set<int>>> set_of_sets;

    for(auto it = x.begin(); it != x.end(); ++it) {
        std::set<int> s;
        s.insert(it->begin(), it->end());
        set_of_sets.insert(s);
    }

    for(auto it = set_of_sets.begin(); it != set_of_sets.end(); ++it) {
        std::cout << "{";
        for(auto it2 = it->begin(); it2 != it->end(); ++it2) {
            std::cout << *it2 << ", ";
        }
        std::cout << "}\n";
    }

    return 0;
}

Using a string list to store final results:

    list<string> uniq_list;
    for (it = mylist.begin(); it != mylist.end(); ++it) {
        set2 = * it; 
        stringstream ss; 
        for (it2 = set2.begin(); it2 != set2.end(); ++it2) {
            ss << * it2 << " ";
        }
        uniq_list.push_back(ss.str());
    }   
    uniq_list.sort();
    uniq_list.unique();
    for (list<string>::iterator it=uniq_list.begin(); it != uniq_list.end(); it++){
      cout << *it << endl;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM