几个向量的笛卡尔乘积 [英] Cartesian product of several vectors

查看:163
本文介绍了几个向量的笛卡尔乘积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]

得到这样的东西:

[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 ++实现这将是有益的。性能不是很大的问题,因为我只需要运行一次算法。

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;

这篇关于几个向量的笛卡尔乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆