Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- void permute( std::vector<T> nums, void (*function)( std::vector<T>& ), int length = -1 )
- {
- if( length < 0 || length > nums.size() )
- length = nums.size();
- std::function<void( int )> genPerms;
- std::vector<T> perm( length, 0 );
- std::vector<bool> used( length, false );
- genPerms = [&function, &length, &nums, &perm, &used, &genPerms]( int n )
- {
- if( length == n )
- {
- function( perm );
- } else
- {
- for( int i = 0; i < nums.size(); i++ )
- {
- if( used[i] )
- continue;
- perm[n] = nums[i];
- used[i] = true;
- genPerms( n+1 );
- used[i] = false;
- }
- }
- };
- genPerms( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement