尝试使用 next_permutation 在 C++ 中模拟 python 组合

Posted

技术标签:

【中文标题】尝试使用 next_permutation 在 C++ 中模拟 python 组合【英文标题】:Trying to simulate python combinations in C++ with next_permutation 【发布时间】:2012-11-05 13:54:21 【问题描述】:

我需要将一个用 Python 编写的 sn-p 移植到 C++ 但是那个 sn-p 使用的是 python 中 itertools 的组合。

我真正有兴趣移植到 C++ 的那一行是:

for k in combinations(range(n-i),2*i):

Python 中的range(n-i) 将从0 to (n-i) - 1 生成一个列表

设 n = 16, i = 5

print range(n-i)

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

python 组合将在该列表中生成所有可能的组合。

例如

print list(combinations(range(n-i),2*i))

输出:

[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 10), (0, 1, 2, 3, 4, 5, 6, 7, 9, 10), (0, 1, 2, 3, 4, 5, 6, 8, 9, 10), (0, 1, 2, 3, 4, 5, 7, 8, 9, 10), (0, 1, 2, 3, 4, 6, 7, 8, 9, 10), (0, 1, 2, 3, 5, 6, 7, 8, 9, 10), (0, 1, 2, 4, 5, 6, 7, 8, 9, 10), (0, 1, 3, 4, 5, 6, 7, 8, 9, 10), (0, 2, 3, 4, 5, 6, 7, 8, 9, 10), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]

我想在 C++ 中使用 std::vectornext_permutation 生成类似的输出,但我仍然得到错误的结果。这是我目前的做法:

for(int j = 0; j < n-i; j++) 
        temp_vector.push_back(j); 

那个 sn-p 相当于 Python 中的range(n-i)

但是下面的sn-p:

do 
     myvector.push_back(temp_vector);
 while(next_permutation(temp_vector.begin(),temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;

不等同于 Python 中的combinations(range(n-i),2*i)),我尝试了很多变体,但仍然无法得出我期望的结果。

例如:

设 n = 16 i = 5

Python

&gt;&gt;&gt; print len(list(combinations(range(n-i),2*i)))

11

C++

#include <vector>
#include <iostream>

using namespace std;
int main() 
    vector<int> temp_vector;
    vector< vector<int> > myvector;
    int n = 16, i = 5;
    for(int j = 0; j < n - i; j++) 
            temp_vector.push_back(j);
    
    do 
            myvector.push_back(temp_vector);
     while(next_permutation(temp_vector.begin(), temp_vector.begin()+2*i));
    cout<<myvector.size()<<endl;
    return 0;

g++ combinations.cpp

./a.out

3628800

任何指导将不胜感激!非常感谢!

【问题讨论】:

【参考方案1】:

组合和排列不是一回事。

组合是另一个集合中项目子集的无序列表。排列是列表中项目的唯一顺序。

您正在从 11 项内容的列表中生成 10 项内容的所有组合,因此您将获得 11 个结果,每个结果都缺少原始 11 项中的一项。

生成每个排列将生成原始 11 项的每个唯一顺序。由于本例中的项目都是唯一的,这意味着结果将是 11!列出每个包含所有 11 个项目的位置。但是,您仅从前 10 个项目生成排列,因此您将获得 10 个!列表,其中不包含第 11 项。

您需要找到一种算法来生成组合而不是排列。


没有用于组合的内置算法。 std::next_permutation 可用作生成组合的算法的一部分:参见Generating combinations in c++。

Here's 组合算法的旧提案草案,包括代码。

【讨论】:

我查看代码已经很久了,以至于我没有意识到 C++ 算法正在生成排列,而不是组合......我现在觉得很愚蠢!谢谢你让我大开眼界! 您是否碰巧知道在 C++ 中生成组合的内置算法?这对我有很大帮助:)

以上是关于尝试使用 next_permutation 在 C++ 中模拟 python 组合的主要内容,如果未能解决你的问题,请参考以下文章

Alamofire 在 swift 2.2 中抛出错误

Alamofire 从多个请求中取消一个请求

请求 Alamofire

发送 Alamofire JSON 数组 Swift

AlamoFire 图像内存问题

Alamofire4在使用方法时抛出“额外参数方法”:.post