Combination

size
int combintation(int n, int r){
if(n==r || r==0) return 1;
else return combination(n-1, r-1)+combination(n-1, r);
}
Combination
#include <iostream>
#include <vector>
using namespace std;
void combination(vector<int> vec, vector<int> comb, vector<vector<int>>& ret, int r, int index, int depth){
if (r==0){
ret.push_back(comb);
} else if (depth==vec.size()) return;
else {
comb[index]=vec[depth];
combination(vec, comb, ret, r-1, index+1, depth+1);
combination(vec, comb, ret, r, index, depth+1);
}
}
int main(void){
vector<int> vec={1, 2, 3, 4, 5};
int r=3;
vector<int> comb(r);
vector<vector<int>> ret;
combination(vec, comb, ret, r, 0, 0);
for(vector<int> tmp_vec: ret){
for(int tmp_int: tmp_vec) cout<<tmp_int<<" ";
cout<<endl;
}
return 0;
}
combination with next_permutation || prev_permutation
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
const int r=2;
vector<int> arr={1, 2, 3, 4};
vector<bool> temp(arr.size(), true);
for(int i=0; i<arr.size()-r; ++i) temp[i]=false;
do{
for(int i=0; i<arr.size(); ++i){
if(temp[i]) cout<<arr[i]<<" ";
}
cout<<endl;
} while(prev_permutation(temp.begin(), temp.end()));
}