I have 3 question -
- How to generate all the subsets?
- How to generate all the subsets of a given length?
- How to generate all the permutations?
How to do this using stl?
I have 3 question -
How to do this using stl?
@mathecodician Here is the code IdeOne - All Subsets to generate all possible subsets of a given array of numbers.
For subsets of a given size , see this code IdeOne - Subsets of a given size : . It is taken from second answer here : c++ - generate all subsets of size k from a set - Stack Overflow . Here’s another link on generating subsets of a given length GeeksForGeeks - All Combinations of R elements from Array of size N
For permutations, I don’t think you will need STL library as shown in the example here (Official Website) . You just sort the given array and use the next_permutation function iteratively to generate all possible subsets. And if want to generate all possible permutations of a given string, you should see the tutorial here : GeeksforGeeks - Print all permutations of a given String
By the way, I found many answers on the Internet for subsets which used Bit Manipulation but I don’t understand that much so I avoided it. However if you are used to with bits, then you may see the topmost links on a google search! (Example : GeeksforGeeks - generating Power Set and All Subsets using Bit Manipulation )
Is there an STL way to do the subsets thing?
Tbh @mathecodician, even I don’t how to do the subset thing using STL.
I don’t think there’s any such function in STL which would help in that.
In python there is the itertools library though
Yea. Generating subsets and permutations using itertools merely reduces to one line or so in Python. Surely a lifesaver!