There is a data type structure:
typedef int MyType; // какой-то тип, int - для примера typedef stf::vector<std::vector<MyType>> Matrix Matrix m = { {1,2,3}, {4,5}, {6,7} } The task is to get the output structure of the form
{ {1,4,6}, {1,4,7}, {2,4,6}, ... {3,4,7}, {3,5,6}, {3,5,7} } Those. a list of all the different vectors in which the 0th element is from the zero row, the 1st one from the first, the ith from the i-th row of the original "matrix". The length of each vector is equal to the number of rows of the incoming structure. Number of vectors = l(0)*l(1)*...*l(N-1) , where N is the number of incoming lines, l (i) is the length of the i-th line. And yes, the lines can be of different lengths. All options for the {{1,2},{3,4}} incoming structure: {{1,3},{1,4},{2,3},{2,4} .
I decided to solve the problem (it is clear that for large sizes of vectors it is expensive to store the result in memory) as follows:
template <typename T> class Permutator { Permutator(std::vector<std::vector<T>> data) { /* ... */ } std::vector<T> next() { /* ... */ } bool end() const { /* возвращает признак перебора всех перестановок */ } void start() { /* обнуляет перестановки - next() вернет {1,4,5} */ } } Using:
Permutator<MyType> p(m); // m - смотри выше while (!p.end()) { std::vector<MyType> v = p.next(); // что-то делаем с v } The question is, actually, in the following, is there something similar in STL?
next_permutationshere. I got the code <60 lines, with O (n) by memory (for the nxm matrix). Without recursion. The complexity is next - O (1). And factorial complexity scares. Simply, maybe it already exists in STL, but I don’t know ... - andy.37