Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- std::vector<std::vector<T>> combinations(const std::vector<T> &vector,
- const size_t &combinations_len)
- {
- std::function<
- void(const std::vector<T>&,
- const size_t&,
- std::vector<std::vector<T>>&,
- std::vector<T>&,
- size_t)
- > __combinations;
- __combinations = [&__combinations]
- (const std::vector<T> &v,
- const size_t &len,
- std::vector<std::vector<T>> &ans,
- std::vector<T> &comb,
- size_t current_index)
- {
- if (comb.size() == len)
- {
- ans.push_back(comb);
- return;
- }
- for (size_t i = current_index + 1; i < v.size(); ++i)
- {
- comb.push_back(v[i]);
- __combinations(v, len, ans, comb, i);
- comb.pop_back();
- }
- };
- std::vector<std::vector<T>> ans;
- std::vector<T> comb;
- __combinations(vector, combinations_len, ans, comb, -1);
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment