如何在 C++ 中使用 STL 为低于总长度的位置数创建排列

kes*_*Him 19 c++ python permutation

我有一个c++ vectorstd::pair<unsigned long, unsigned long>对象。我正在尝试使用std::next_permutation(). 但是,我希望排列具有给定的大小,您知道,类似于permutationspython 中指定预期返回排列大小的函数。

基本上,c++相当于

import itertools

list = [1,2,3,4,5,6,7]
for permutation in itertools.permutations(list, 3):
    print(permutation)
Run Code Online (Sandbox Code Playgroud)

Python 演示

(1, 2, 3)                                                                                                                                                                            
(1, 2, 4)                                                                                                                                                                            
(1, 2, 5)                                                                                                                                                                            
(1, 2, 6)                                                                                                                                                                            
(1, 2, 7)                                                                                                                                                                            
(1, 3, 2)
(1, 3, 4)
..
(7, 5, 4)                                                                                                                                                                            
(7, 5, 6)                                                                                                                                                                            
(7, 6, 1)                                                                                                                                                                            
(7, 6, 2)                                                                                                                                                                            
(7, 6, 3)                                                                                                                                                                            
(7, 6, 4)                                                                                                                                                                            
(7, 6, 5) 
Run Code Online (Sandbox Code Playgroud)

Jar*_*d42 6

您可能会使用 2 个循环:

  • 取每个 n 元组
  • 迭代那个 n 元组的排列
template <typename F, typename T>
void permutation(F f, std::vector<T> v, std::size_t n)
{
    std::vector<bool> bs(v.size() - n, false);
    bs.resize(v.size(), true);
    std::sort(v.begin(), v.end());

    do {
        std::vector<T> sub;
        for (std::size_t i = 0; i != bs.size(); ++i) {
            if (bs[i]) {
                sub.push_back(v[i]);
            }
        }
        do {
            f(sub);
        }
        while (std::next_permutation(sub.begin(), sub.end()));
    } while (std::next_permutation(bs.begin(), bs.end()));
}
Run Code Online (Sandbox Code Playgroud)

演示

  • @d4rk4ng31:我们确实只遇到每个排列一次。[`std::next_permutation`](https://en.cppreference.com/w/cpp/algorithm/next_permutation) 的复杂性“不清楚”,因为它计算交换(线性)。子向量的提取可以改进,但我认为它不会改变复杂性。此外,排列数取决于向量大小,因此两个参数不是独立的。 (2认同)