fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void func(int idx, vector<int>& v, vector<vector<int>>& ans, vector<int>& temp) {
  5. if (idx == (int)v.size()) {
  6. ans.push_back(temp);
  7. return;
  8. }
  9.  
  10. // Include current element
  11. temp.push_back(v[idx]);
  12. func(idx + 1, v, ans, temp);
  13.  
  14. // Backtrack: remove the last element
  15. temp.pop_back();
  16.  
  17. // Exclude current element
  18. func(idx + 1, v, ans, temp);
  19. }
  20.  
  21. int main() {
  22. vector<int> v = {3, 1, 2};
  23. vector<int> temp;
  24. vector<vector<int>> ans;
  25.  
  26. func(0, v, ans, temp);
  27.  
  28. for (const auto& seq : ans) {
  29. for (int num : seq)
  30. cout << num << " ";
  31. cout << endl;
  32. }
  33. }
  34.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
3 1 2 
3 1 
3 2 
3 
1 2 
1 
2