C++: Binary method to generate subsets

    A set of n elements (n >=0, n is an integer) can generate 2^n subsets. For example, {a,b} can generate 4 subsets {empty}, {a},{b}{a,b}

The binary method is to use binary representation from 0 to 2^n, and the corresponding array position element of 1 is included in the subset set.

For example, a = {a,b,c,d} has 16 subsets, create the following table

Decimal Binary number 1 corresponding array element Result set
0 0000   air
1 0001 a[3] d
2 0010 a[2] c
3 0011 a[2], a[3] c,d
4 0100 a[1] b
5 0101 a[1],a[3] b,d
6 0110 a[1],a[2] b,c
7 0111 a[1],a[2],a[3] b,c,d
8 1000 a[0] a
9 1001 a[0],a[3] a,d
10 1010 a[0],a[2] a,c
11 1011 a[0],a[2],a[3] a,c,d
12 1100 a[0],a[1] a,b
13 1101 a[0],a[1],a[3] a,b,d
14 1110 a[0],a[1],a[2] a,b,c
15 1111 a[0],a[1],a[2],a[3] a,b,c,d

 

 

 /**
         * <p> 获取所有子集
         * @tparam ElemType  返回值类型
         * @tparam _InputIterator 线性表的迭代器类型
         * @param _first1      线性表的起始地址
         * @param _last1    线性表的结束地址
         * @param _null     用变量来指定返回值类型避免编译不过
         * @return  所有子集
         */
        template<class ElemType,class  _InputIterator>
        vector<vector<ElemType>> makeSubset(_InputIterator _first1, _InputIterator _last1, ElemType _null ){
            int n = _last1 - _first1;
            vector<vector<ElemType>> ret;
            // 2的n次方   1<<n
            for(int s=0;s<(1<<n);s++) {
                vector<ElemType> subRet;
                for (int i = 0; i < n; i++) {
                    if (s & (1 << i)) //1左移i位,监测s的哪一位为1,为1的话输出
                    {
                        subRet.push_back(*(_first1 + i));
//                        std::cout<<i;
                    }
                }
                ret.push_back(subRet);
//                std::cout << std::endl;
            }
            return  ret;
        }

Call example:

#include<algorithm>
#include<iostream>
using namespace std;
int main(){
   string aaaa[3]={"7a8","2b2","3c1"};
    string string1;
    vector<vector<string>>aaRet= makeSubset(aaaa, aaaa + 3, string1);
    for (int j = 0; j < aaRet.size(); ++j) {
        cout<<"{";
        for (int i = 0; i < aaRet[j].size(); ++i) {
            cout<< aaRet[j][i] <<" ";
        }
        cout<<"}\n";
    }

}

 

{}
{7a8}
{2b2}
{7a8 2b2}
{3c1}
{7a8 3c1}
{2b2 3c1}
{7a8 2b2 3c1}

 

 

 

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/109440389