Cartesian product of several vectors(几个向量的笛卡尔积)
问题描述
之前有人问过类似的问题,但我找不到与我的问题完全匹配的问题.
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;
这篇关于几个向量的笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:几个向量的笛卡尔积
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01