0
votes

I want to generate combination of vectors of a vectors of vectors. For ex: if

std::vector<std::vector<int>> arr = {{1,2},{1,3},{1,4},{1,5}}

Comb(arr, 2) shall return:

{
{{1,2},{1,3}},{{1,2},{1,4}}, {{1,2},{1,5}}, {{1,3},{1,4}}, {{1,3},{1,5}}, {{1,4},{1,5}}
}

(i.e, considering vectors as objects that needs to be combinated)

The code below does combinations for integers, I beleive I can get what I need by modifying this code: for ex. by starting to change type of function to vector<vector<vector> since we have to return vector of vector of vectors.

Thanks everyone that shows interest in this question.

std::vector<std::vector<int>> Combinations::combs(int n, int r) {
    std::vector<bool> v(n);
    std::fill(v.end() - r, v.end(), true);
    std::vector<std::vector<int>> sol;
    do {
        std::vector<int> row;
        for (int i = 0; i < n; ++i) {
            if (v[i]) {
                row.push_back(i+1);
            }
        }
        sol.push_back(row);
    } while (std::next_permutation(v.begin(), v.end()));
    return sol;
}
1

1 Answers

1
votes
std::vector<std::vector<std::vector<int>>> Combinations::combs(std::vector<std::vector<int>> const &vecs, int r) {
    std::vector<bool> v(vecs.size());
    std::fill(v.end() - r, v.end(), true);
    std::vector<std::vector<std::vector<int>>> sol;
    do {
        std::vector<std::vector<int>> row;
        for (int i = 0; i < vecs.size(); ++i) {
            if (v[i]) {
                row.push_back(vecs[i]);
            }
        }
        sol.push_back(row);
    } while (std::next_permutation(v.begin(), v.end()));
    return sol;
}