几个向量的笛卡尔积

2021-12-21 00:00:00 math combinations unique vector c++

之前有人问过类似的问题,但我找不到与我的问题完全匹配的问题.

similar questions have been asked before but I cant find an exact match to my question.

我有 4 个向量,每个向量包含 200-500 个 4 位整数.每个向量中元素的确切数量各不相同,但我可以将其固定为特定值.我需要找到这 4 个向量中元素的所有可能组合.

I have 4 vectors each of which hold between 200-500 4 digit integers. The exact number of elements in each vector varies but I could fix it to a specific value. I need to find all possible combinations of the elements in these 4 vectors.

例如:

v1[10, 30]v2[11, 45]v3[63, 56]v4[82, 98]

v1[10, 30] v2[11, 45] v3[63, 56] v4[82, 98]

所以我会得到这样的东西:

so I'd get something like this:

[10, 11, 63, 82];[30、11、63、82];[10、45、63、82];[10, 45, 56, 82] 等.

[10, 11, 63, 82]; [30, 11, 63, 82]; [10, 45, 63, 82]; [10, 45, 56, 82] etc..

这个算法是否有一个通用名称,以便我可以在网上找到一些参考资料?否则,在 C++ 中实现这一点的任何提示都会有所帮助.性能不是什么大问题,因为我只需要运行一次算法.STL 中是否有任何内置内容?

Is there a common name for this algorithm so I can find some references to it online? Otherwise any tips on implementing this in C++ would be helpful. Performance isn't much of an issue as I only need to run the algorithm once. Is there anything built into the STL?

推荐答案

算法不多...

for(vector<int>::const_iterator i1 = v1.begin(); i1 != v1.end(); ++i1)
    for(vector<int>::const_iterator i2 = v2.begin(); i2 != v2.end(); ++i2)
        for(vector<int>::const_iterator i3 = v3.begin(); i3 != v3.end(); ++i3)
            for(vector<int>::const_iterator i4 = v4.begin(); i4 != v4.end(); ++i4)
                cout << "[" << *i1 << "," << *i2 << "," << *i3 << "," << *i4 << "]" << endl;

相关文章