I was reading this stackoverflow post TMP: how to generalize a Cartesian Product of Vectors? and I am very intrigued by the simplicity of the template and that it doesn't require moves or copies. However, in my case, I have a vector of classes which have the arrays to perform the cross product of; so I cannot specify cross( a, b, c ), and loping through and doing cross( b, c) then crossing that with a provides a nested set.
How can I perform the same, but use a vector instead of knowing the parameters a priori [i.e. cross(vector) ]?
Better yet....can it be done with a stack rather than recursive calls?
Here is my class setup:
#include<vector>
#include<memory>
class Item {
public:
std::string name;
std::string id;
};
class ItemList {
public:
std::vector<Item> items;
};
int main() {
std::vector<std::shared_ptr<ItemList>> lists;
// Load some sample data
for( size_t list_idx = 0; list_idx < 3; list_idx++ ) {
auto list = std::make_shared<ItemList>();
for( size_t item_idx = 0; item_idx < 5; item_idx++ ) {
Item item;
item.name = "List " + std::to_string(list_idx) + " - Item " + std::to_string(item_idx);
item.id = item.name;
list->items.push_back( item );
}
lists.push_back(list);
};
// Now, how to perform the cross product of the items???
// cross( a, b, c ) is not the same as cross( a, cross( b, c ))
}
EDIT: Clarification on what I have as inputs and outputs:
So if I have a vector of 3 lists where the following are stored in the name member of the Item class.
A1, A2, A3
B1, B2
C1, C2, C3, C4
I am trying to return
{ A1, B1, C1 }
{ A1, B1, C2 }
{ A1, B1, C3 }
{ A1, B1, C4 }
{ A1, B2, C1 }
{ A1, B2, C2 }
{ A1, B2, C3 }
{ A1, B2, C4 }
{ A2, B1, C1 }
{ A2, B1, C2 }
{ A2, B1, C3 }
{ A2, B1, C4 }
{ A2, B2, C1 }
{ A2, B2, C2 }
{ A2, B2, C3 }
{ A2, B2, C4 }
{ A3, B1, C1 }
{ A3, B1, C2 }
{ A3, B1, C3 }
{ A3, B1, C4 }
{ A3, B2, C1 }
{ A3, B2, C2 }
{ A3, B2, C3 }
{ A3, B2, C4 }
EDIT2: The link provided by @Matt McNabb is along the right track, but I can't use boost, so I am not sure how to modify this appropriately without it. Also, since I have a vector<shared_ptr<vector<>>>
rather than vector<vector<>>
, it complicates this a bit more. Although, I could generate a vector<vector>
.
Thoughts?