C ++ STL下一个排列与组合 [英] C++ STL Next Permutation with Combination
问题描述
我知道我可以在包含元素 [1,2,3] $ c $的某个容器上使用
将是从这个标准产生的排列之一。我正在寻找一个STL的方法(如果可能),而不是写我自己的组合功能。任何特定的STL实现我应该阅读关于 std :: next_permutation
c>其将产生该序列的6个排列。我想要做的是给一些 [1,2,3,4,5,6]
生成大小为3的所有可能的排列。 code> [4,3,2]
I know that I can use std::next_permutation
on some container containing the elements [1, 2, 3]
which would generate 6 permutations of this sequence. What I would like to do is given some set [1, 2, 3, 4, 5, 6]
generate all possible permutations of size 3. So for this example, [4, 3, 2]
would be one of the permutations resulting from this criteria. I'm looking for an STL way of doing this (if possible) rather than writing my own combinations function. Any particular STL implementation I should be reading about ?
推荐答案
这不是最有效的可能算法,但它很容易。您必须从排序的元素开始。为了获得下一个k置换,逆转最后的n-k个元素,然后尝试获得下一个置换。前k个元素是下一个k置换。
This is not the most efficient possible algorithm but it's easy. You must start with the elements sorted. To get the next k-permutation, reverse the last n-k elements and then try to get the next permutation. The first k elements are the next k-permutation.
这篇关于C ++ STL下一个排列与组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!