-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations_k.cpp
More file actions
47 lines (44 loc) · 1.11 KB
/
combinations_k.cpp
File metadata and controls
47 lines (44 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <vector>
#include <iostream>
// requires integer type
template<typename Item_type>
void combinations_k(
std::vector<Item_type> &items,
std::size_t position,
Item_type next,
std::size_t combo_size,
std::size_t num_elems,
std::vector<std::vector<Item_type>> &out
)
{
if (position == combo_size) {
out.push_back({items.begin(), items.end()});
return;
}
for (auto i = next; i < num_elems; ++i) {
items[position] = i;
combinations_k(items, position + 1, i + 1, combo_size, num_elems, out);
}
}
template<typename Item_type>
std::vector<std::vector<Item_type>> combinations_k(
std::size_t combo_size,
std::size_t num_elems
)
{
std::vector<Item_type> items(combo_size, 0);
std::vector<std::vector<Item_type>> out;
combinations_k(items, 0, 0, combo_size, num_elems, out);
return out;
}
int main()
{
auto out = combinations_k<int>(3, 5);
for (const auto &v : out) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << "\n";
}
return 0;
}