0

Is there an easy way in C++17 to generate variations from a given vector of elements?

void get_variations(int len, const std::vector<int> &array, std::vector<std::vector<int>> &result) {
    // ...
}

int main() {
    std::vector<std::vector<int>> result;
    std::vector<int> array = {1, 2, 3};

    get_variations(2, array, result);

    for(auto variation : result) {
        for(auto element : variation) {
            std::cout << element << ' ';
        }
        std::cout << std::endl;
    }
}

Output should be:

1 2
1 3
2 1
2 3
3 1
3 2
janw
  • 8,758
  • 11
  • 40
  • 62
Parzival
  • 55
  • 6

0 Answers0