fork download
  1. #include<iostream>
  2. #include<list>
  3. #include <iterator>
  4.  
  5. void print( std::list<int> l){
  6. std::copy( l.begin(), l.end(), std::ostream_iterator<int>( std::cout, " "));
  7. std::cout << std::endl;
  8. }
  9.  
  10. void subset( int arr[], int size, int left, int index, std::list<int> &l){
  11. if( left == 0){
  12.  
  13. print(l);
  14. return;
  15. }
  16.  
  17. for( int i = index; i < size; i++){
  18. l.push_back( arr[i]);
  19. subset( arr, size, left - 1, i + 1, l);
  20. l.pop_back();
  21. }
  22.  
  23. }
  24.  
  25. int main() {
  26.  
  27. int array[5] = { 1, 2, 3, 4, 5} ;
  28. std::list<int> lt;
  29. subset( array, 5, 1, 0, lt);
  30. subset( array, 5, 2, 0, lt);
  31. subset( array, 5, 3, 0, lt);
  32. subset( array, 5, 4, 0, lt);
  33. subset( array, 5, 5, 0, lt);
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1 
2 
3 
4 
5 
1 2 
1 3 
1 4 
1 5 
2 3 
2 4 
2 5 
3 4 
3 5 
4 5 
1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 
1 2 3 4 
1 2 3 5 
1 2 4 5 
1 3 4 5 
2 3 4 5 
1 2 3 4 5