Reputation: 4941
I understand the algorithm for doing this but I don't know what data structure (array, linked list, vector, other?) would be best for returning the final set of sets since every example I see just asks to print the sets.
To be clear, I mean ALL subsets, so they have different sizes.
Upvotes: 0
Views: 532
Reputation: 61910
Once i used bit fields to determine the subsets. Where if the i
th bit is 1
then the i
the element in the set is selected in the subset and 0
otherwise. In this case you need to store the ordering of the elements. The same can be done with bool
vectors i think.
Upvotes: 1
Reputation: 206518
The decision of which data structure to use depends on:
A normal array, would give you contiguous block of memory and random access to the elements, however you need to know the exact number of elements before hand so that you can allocate an array of appropriate size.
With std::vector
you get random access to the data, and contiguous just like arrays but vector is a dynamic array,it grows as you add new elements, and a amortized constant complexity, however insertion/deletion of elements is faster only at the ends since all elements need to be moved.
With std::list
you don't get the random access but insertion and deletion of elements is faster because it involves just moving around of the pointer links.
Also, are vectors even used anymore?
That is not true at all.
They are very much in use and one of the most widely used data structures provided by the Standard Library.
Upvotes: 2