Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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++ - Fatal编程技术网

C++ 使用向量迭代创建集合的子集

C++ 使用向量迭代创建集合的子集,c++,C++,所以我想创建一个子集,通过代码运行该子集,然后创建一个新的子集。我用向量表示集合和子集。到目前为止,我有3个嵌套的for循环,但是我很难找出我需要的变量 这是我想做的。set={0,1,2,3,4,5}这个值与索引匹配,只是为了简化这个例子。我现在想要子集={}->{0}->{1}->…->{0,1} -> {0,2} -> ... -> {0,5} -> {0,1,2} -> ... -> {0,4,5}. 我很难用变量来表示条件 基本上,我希望第一个for循环增加子集的大小。从0到set.s

所以我想创建一个子集,通过代码运行该子集,然后创建一个新的子集。我用向量表示集合和子集。到目前为止,我有3个嵌套的for循环,但是我很难找出我需要的变量

这是我想做的。set={0,1,2,3,4,5}这个值与索引匹配,只是为了简化这个例子。我现在想要子集={}->{0}->{1}->…->{0,1} -> {0,2} -> ... -> {0,5} -> {0,1,2} -> ... -> {0,4,5}. 我很难用变量来表示条件

基本上,我希望第一个for循环增加子集的大小。从0到set.size()(这很简单)。在该循环中,我希望有一个迭代器对应于子集元素中的索引。我将这个迭代器初始化为subset.size(),这样我们就可以先处理最后一个元素,然后再处理子集中的第一个元素。然后是第三个for循环,我想在集合中的可能值之间迭代。假设我们的当前子集={0,1,2}我如何让我的程序知道将值“2”放在子集的最后一个元素中,然后是1然后是0

我想这会涉及到从set.size()-1和subset.size()-1中获取差异的问题?但我不太清楚怎么做。然后我想迭代到{0,1,5},然后是{0,4,5},但是我不知道如何告诉程序停在4,而不是5。我再次认为这是有区别的,但我不能完全理解

重述:

for loop to iterate through subset size
    for loop to iterate through subset "working" element, starting from back
        for loop to iterate through that index of subset,
        starting from the correct corresponding set value to ending
        at the correct corresponding set value

使得子集从{}->{0}->{1}->…->{0,1}->{4,5}->{1,2,3}->…->{1,4,5}我实际上不需要子集={1,2,3,4,5},但是如果我不能在那之前停止,它不会伤害我的代码。同样,我希望将起点和终点表示为变量,以使内部循环工作,但我无法理解。非常感谢所有能帮助我的人。

这大概就是我要做的

//handle null subset
for ( int size = 1; size < n; i++ ) {
    int indices[size];
    for ( int i = 0; i < size; i++ ) indices[i] = i;

    while ( indices[0] <= n - size ) {
        int i;
        for ( i = 1; indices[size - i] == n - i; i-- );
        indices[i]++;
        for ( i = i + 1; i < size; i++ ) indices[i] = indices[i-1] + 1;
        //print out elems using the indices in `indices`
    }
    //done with all subsets of size `size`
}
//处理空子集
对于(int size=1;size而(索引[0]枚举所有子集的技巧是排列一个“选择标志”数组,其中的每个元素指示是否选择了原始数组中的对应元素

以下是示例代码:

void foo(const vector<int>& a)
{
    size_t size = a.size();

    // selection flag array
    // '1' indicates selected, '0' indicates unselected
    vector<int> f(size, 0);

    for (size_t i = 1; i <= size; i++)
    {
        // increase the count of selected elements
        f[i - 1] = 1;

        do
        {
            for (size_t i = 0; i < size; i++)
            {
                if (f[i])
                {
                    printf("%d\t", a[i]);
                }
            }
            printf("\n");

        } while (next_permutation(f.begin(), f.end(), [](int a, int b){ return a > b; }));
        // next_permutation tries to permutate the array
        // i.e. '1 1 0 0' -> '1 0 1 0' -> '0 1 1 0' -> ... -> '0 0 1 1'(end)
    }
}
void foo(常量向量&a)
{
尺寸=a.尺寸();
//选择标志数组
//“1”表示选中,“0”表示未选中
向量f(大小,0);
对于(size_t i=1;i b;}));
//下一步\u排列尝试排列数组
//即"1110"-->"1010"-->"0110"-->.->"0011"(完)
}
}