求一个集合的全部子集 假设都是Int类型的元素

vector<int> int2bin_vec(int n, UL bit_num){
    vector<int> res(bit_num, 0);
    auto it = res.begin() + res.size() - 1;
    while( n != 0){
        *it = (n % 2);
        it--;
        n /= 2;
    }
    return res;
}

vector<int> zero_one2subset(const vector<int>& zero_one, const vector<int>& nums){
    if(zero_one.empty() || nums.empty() || zero_one.size() != nums.size()) return {};
    vector<int> res;
    for(int i = 0; i < zero_one.size(); i++){
        if(zero_one[i] == 1){
            res.emplace_back(nums[i]);
        }
    }
    return res;
}


vector<vector<int>> cal_subset(const vector<int>& nums){
    int subset_size = (1 << nums.size());
    vector<vector<int>> res((UL)subset_size);
    for(int i = 0; i < subset_size; i++){
        vector<int> zero_one = int2bin_vec(i, nums.size());
        vector<int> subset = zero_one2subset(zero_one, nums);
        res[i] = subset;
    }
    sort(res.begin(), res.end());
    return res;
}
原文地址:https://www.cnblogs.com/theodoric008/p/9601114.html