How to make possible combinations between elements of sets?
Make a chain long in the number of sets, each of which includes one element from each set. All sets are integer and the values of the elements of the sets are in order (0, 1, 2, etc.)
enum ПЕРВАЯ { А1, А2 }; enum ВТОРАЯ { Б1, Б2 }; enum ТРЕТЬЯ { В1, В2, В3 }; Чтобы получилось так: А1 - Б1 - В1 А1 - Б1 - В2 А1 - Б1 - В3 А1 - Б2 - В1 А1 - Б2 - В2 А1 - Б2 - В3 А2 - Б1 - В1 А2 - Б1 - В2 А2 - Б1 - В3 А2 - Б2 - В1 А2 - Б2 - В2 А2 - Б2 - В3 // Допустим данные по всем enum хранятся так struct МНОЖЕСТВО { int первый; int последний; }; std::vector<МНОЖЕСТВО> вектор; вектор.resize(3); // ПЕРВАЯ вектор[0].первый = (int)ПЕРВАЯ::А1; вектор[0].последний = (int)ПЕРВАЯ::А2; // ВТОРАЯ вектор[1].первый = (int)ВТОРАЯ::Б1; вектор[1].последний = (int)ВТОРАЯ::Б2; // ТРЕТЬЯ вектор[2].первый = (int)ТРЕТЬЯ::В1; вектор[2].последний = (int)ТРЕТЬЯ::В3;
How can I get all the combinations?
Without 1C style. It works only for sets, where all elements are strictly in order.
struct enum_maker { struct enum_class { int first; int last; std::string name; }; std::vector<enum_class> tree; void add_enum(int first, int last , std::string name) { enum_class enum_ob; enum_ob.first = first; enum_ob.last = last; enum_ob.name = name; tree.push_back(enum_ob); } void build_tree() { int iterations = 1; int enums = tree.size(); std::vector<std::vector<int>> matrix; matrix.resize(enums); std::vector<int> elements_in_enum; for(int i = 0; i < tree.size(); i++) { elements_in_enum.push_back((tree[i].last - tree[i].first)+1); iterations *= elements_in_enum[i]; } int special_A = iterations; // заполняем матрицу for(int i = 0; i < tree.size(); i++) { special_A = special_A / elements_in_enum[i]; int count_elements=0; int current_enum_element = tree[i].first; for(int j = 0; j < iterations; j++) { if(count_elements == special_A){current_enum_element++; if(current_enum_element>tree[i].last) { current_enum_element=tree[i].first; } count_elements=0; } count_elements++; matrix[i].push_back(current_enum_element); } } for(int i = 0; i < tree.size(); i++) { std::cout << "\n\n\n#### I=" << tree[i].name; for(int j = 0; j < matrix[i].size(); j++) { std::cout << "\nj=" << matrix[i][j] ; } } for(int i = 0; i < matrix[0].size(); i++) { std::string pairs; for(int j = 0; j < tree.size(); j++) { pairs += "[" + std::to_string( matrix[j][i]) + "]"; } std::cout << "\npairs=" << pairs; } } }; enum FIRST {A1,A2,A3,A4}; enum SECOND {B1,B2}; enum THIRD {C1,C2,C3}; int main() { enum_maker enum_maker_; enum_maker_.add_enum(FIRST::A1 , FIRST::A4 , "FIRST"); enum_maker_.add_enum(SECOND::B1 , SECOND::B2, "SECOND"); enum_maker_.add_enum(THIRD::C1 , THIRD::C3, "THIRD"); enum_maker_.build_tree(); std::system("pause"); }
N
), useN
nested loops. If not, do this: 1. Start the set of current elements, one by one into a set 2. Cycle: 3. Current set: = first 4. Advance the current element of the current set. 5. If at the same time you have rested against the maximum, reset the current element of the current set to the initial value and advance the number of the current set; if at the same time they are also rested against the maximum, it is ready, otherwise return to step 4. 6. Process the current combination, the end of the loop iteration is VladD