Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 生成非连续组合。_C++_Combinations_Combinatorics - Fatal编程技术网

C++ 生成非连续组合。

C++ 生成非连续组合。,c++,combinations,combinatorics,C++,Combinations,Combinatorics,我有一个函数,可以生成向量中的元素组合。组合的数量通常在10000以下,因此非常小。我必须找到解决某个问题的组合。如果一个组合失败,那么类似的组合也很可能失败(尽管不确定)。因此,测试非常不同的组合是有意义的:例如[7 1 3]然后[9 5 2]等等 目前我使用代码from(链接在这里)来生成所有组合。然后我洗牌,然后尝试,直到找到一个有效的组合。最好只是创建一个组合,尝试一下,如果失败,再创建一个(这是非常不同的) 有没有解决这个问题的好办法 #include <vector> #

我有一个函数,可以生成
向量中的元素组合。组合的数量通常在10000以下,因此非常小。我必须找到解决某个问题的组合。如果一个组合失败,那么类似的组合也很可能失败(尽管不确定)。因此,测试非常不同的组合是有意义的:例如[7 1 3]然后[9 5 2]等等

目前我使用代码from(链接在这里)来生成所有组合。然后我洗牌,然后尝试,直到找到一个有效的组合。最好只是创建一个组合,尝试一下,如果失败,再创建一个(这是非常不同的)

有没有解决这个问题的好办法

#include <vector>
#include <algorithm>
#include <iostream>

#include "combinatorics.h"

using namespace std;

int main()
{
    vector<int> v(5);
    iota(begin(v), end(v), 0);

    /* Fill combos with all combinations: choose 3 from v */
    vector<vector<int>> combos;
    auto f = [&] (auto a, auto b)
    {
        combos.emplace_back();
        copy(a, b, back_inserter(combos.back()));
        return false;
    };
    for_each_combination(begin(v), begin(v)+3, end(v), f);

    /* Print all combinations */
    for(auto& combo : combos)
    {
        for(auto i : combo)
            cout<<i<<" ";
        cout<<endl;
    }

    /* Shuffle combos and start trying one combination after the other. */
}
#包括
#包括
#包括
#包括“combinations.h”
使用名称空间std;
int main()
{
向量v(5);
iota(开始(v)、结束(v)、0);
/*用所有组合填充组合:从v中选择3*/
向量组合;
自动f=[&](自动a、自动b)
{
组合。放置回();
复制(a,b,back_插入器(combos.back());
返回false;
};
对于每个_组合(开始(v),开始(v)+3,结束(v),f);
/*打印所有组合*/
用于(自动和组合:组合)
{
用于(自动i:组合)
库特