A Helpful Line-by-Line Code Tutorial
Generate Subsets using Recursion in C++
#include<iostream> #include<vector> using namespace std; int print_vector(vector<int> arr){ for(int i = 0; i < (int) arr.size(); i++){ cout << arr[i] << " "; } cout << "\n"; return 0; } int generate_subsets(vector<int> arr, int n, int k){ if(k > n){ print_vector(arr); return 0; } else { arr.push_back(k); generate_subsets(arr, n, k + 1); arr.pop_back(); generate_subsets(arr, n, k + 1); } return 0; } int main(int argc, char const *argv[]) { vector<int> arr; generate_subsets(arr, 3, 1); return 0; }