多个向量的笛卡尔积

发布于 2024-08-24 07:33:07 字数 403 浏览 2 评论 0原文

类似的问题以前曾被问过,但我找不到与我的问题完全匹配的问题。

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

例如:

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

所以我会得到这样的结果:

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

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

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

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.

eg:

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] etc..

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-08-31 07:33:07

算法不多...

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;

Not much of an algorithm...

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文