fork(23) download
#include 
#include 
 
using namespace std;
 
#define SET_SIZE 5
 
int set[] = {1, 2, 3, 4, 5};
vector > all_combinations;
const int tuple_size = 3;
 
void recursive_comb(int step_val, int array_index, std::vector tuple)
{
    if (step_val == 0)
    {
        all_combinations.push_back(tuple); //<==We have the final combination
        return;
    }
 
    for (int i = array_index; i < SET_SIZE; i++)
    {
        tuple.push_back(set[i]);
        recursive_comb(step_val - 1, i + 1, tuple);
        tuple.pop_back();
    }
 
    return;
}
 
void init_combinations()
{
    std::vector tuple;
    tuple.reserve(tuple_size); //avoids needless allocations
    recursive_comb(tuple_size, 0, tuple);
}
 
int main()
{
    init_combinations();
    cout << "Total Combinations: " << all_combinations.size() << endl;
 
    for (int i=0; i < all_combinations.size(); i++)
    {
        cout << "{";
        for (int j=0; j < tuple_size; j++)
        {
            cout << all_combinations[i][j] << " ";
        }
        cout << "}" << endl;
    }
 
    return 0;
}
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
Total Combinations: 10
{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 }