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;
这篇关于几个向量的笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:几个向量的笛卡尔积
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
