c++ - 生成给定集合的子集

标签 c++ vector

这是我生成给定集合的子集的努力

#include <cstdlib>
#include <iostream>
#include<vector>
using namespace std;

vector<int> getsubset(vector<int> &sets)
{

       vector<int>allsubset;
       int max=1<<sets.size();
        for (int i=0;i<max;i++){
             vector<int> subset;
             int j=i;
             int index=0;
             while( j>0){
             if ((j&1)>0)
             subset.push_back(sets[index]);
             j>>=1;
             index++;       




                    }


              allsubset.push_back(subset);           

            }

         return allsubset;
       }
int main(int argc, char *argv[])
{
    return 0;
}

但是这里有错误

1>------ Build started: Project: subset, Configuration: Debug Win32 ------
1>  subset.cpp
1>c:\users\daviti\documents\visual studio 2010\projects\subset\subset\subset.cpp(27): error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'int &&'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          Reason: cannot convert from 'std::vector<_Ty>' to 'int'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我完全不明白错误,请帮助我,哪里出了问题?

最佳答案

您正在尝试 push_back vector<int>vector<int>容器……没有意义。 你可以做的是插入 intvector<int>容器。

我会更正 allsubset 的定义来自 vector <int>vector< vector<int> >

关于c++ - 生成给定集合的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9585758/

相关文章:

c++ - 引用一个对象而不在 std::vector 中为其创建变量是否会创建该对象的新实例?

c++ - 游戏GUI库

C++ 将 vector<string> 的排序内容写入文件

c++ - const 和 STL 容器

C++ UNIX 错误 : Undefined first referenced symbol in file

vector - 如何从矢量创建 VecDeque?

R - 获取向量中最大 n 个元素的索引的最快方法

c++ - vector.erase 和 std::remove 自定义 vector

c++ - TinyXML 无法将属性与字符进行比较

c++ - 在 C++ 中使用 OpenCV 2.4 计算凸性缺陷