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;
这篇关于几个向量的笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:几个向量的笛卡尔积
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
