I am trying to create a powerset template which permits both the input set (in the mathematical sense except that duplicate members are permitted) and output set of sets to be any container.
To do this efficiently it seems necessary to know what sort of containers we are dealing with. Can we do that? How might we best do that?
Here is what I have written. It should be pointed out that I do not know if the rest of the template is correct (some is my best guess at what will work), since this problem has stumped me before I started testing, but it should suffice to give an idea. The problem is explained in the comments in terms of whether the containers are sets or vectors, but I would like a general solution for any container (including custom ones, at least if they meet fairly simple specifications).
//Data_ContB must = Some_Container_Type<Data_ContA>
template <class Data_ContA,class Data_ContB>
Data_ContB Powerset(Data_ContA In,Data_ContB& Out)
{
if (In.size()==0)
{
Data_ContA empty;
powA.push_back(empty);
}
else
{
Data_ContA::reverse_iterator i=In.rbegin();
typename allocator<Data_ContA> taken=*i;
In.erase(In.rbegin);
Data_ContB powA;
Powerset<Data_ContA,Data_ContB>(In,powA);
Data_ContB powB;
Data_ContB::iterator l!=powA.end()
for (Data_ContB::iterator i=powA.begin();i!=l;++i)
{
Data_ContA next=*i;
/* Oh no...
if next is vector: next.push_back(taken);
if next is set: next.insert(taken);
if powB is vector: powB.push_back(next);
if powB is set: powB.insert(next);
*/
}
/* And again...
if powA is vector: powA.insert(powA.end(),powB.begin(),powB.end());
if powA is set: powA.insert(powB.begin(),powB.end());
*/
}
return powA;
}
An interface consisting of iterators is more flexible than an interface taking containers. Consider, for example, std::copy().
It takes a pair of input iterators that define the range of elements to copy and a third output iterator to where to copy.
This solves your insert/push_back problem.