使用STL的next_permutation函数生成全排列(C++)

使用STL的next_permutation函数生成全排列(C++)

2009年3月28日 14:39 4,944 次阅读 发表评论 阅读评论

文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

下午研究了一下全排列算法,然后发现C++的STL有一个函数可以方便地生成全排列,这就是next_permutation

在C++ Reference中查看了一下next_permutation的函数声明:

#include <algorithm>
bool next_permutation( iterator start, iterator end );

The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

从说明中可以看到 next_permutation 的返回值是布尔类型。按照提示写了一个标准C++程序:

#include <iostream>
#include <algorithm>
#include <string>
 
using namespace std;
 
int main()
{
    string str;
    cin >> str;
    sort(str.begin(), str.end());
    cout << str << endl;
    while (next_permutation(str.begin(), str.end()))
    {
        cout << str << endl;
    }
    return 0;
}


其中还用到了 sort 函数和 string.begin()、string.end() ,函数声明如下:

#include <algorithm>
void sort( iterator start, iterator end );

sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。

#include <string>
iterator begin();
const_iterator begin() const;

#include <string>
iterator end();
const_iterator end() const;

string.begin()和string.end() 可以快速访问到字符串的首字符和尾字符。

在使用大数据测试的时候,发现标准C++的效率很差...换成C函数写一下,效率提升了不止一倍...

#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100
 
using namespace std;
 
int main()
{
    int length;
    char str[MAX];
    gets(str);
    length = strlen(str);
    sort(str, str + length);
    puts(str);
    while (next_permutation(str, str + length))
    {
        puts(str);
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C++中的next_permutation函数是一个STL算法,用于生成下一个排列。它接受两个迭代器作为参数,表示一个范围内的元素。函数会将这个范围内的元素重新排列生成下一个排列,并返回true。如果已经是最后一个排列,则返回false。 ### 回答2: C++标准库中的next_permutation函数用于生成一个序列中下一个排列的序列。 该函数在一个非递减排列的序列中,找到下一个比当前排列更大的排列,并对原序列进行重排,如果不存在下一个更大的排列,则返回false。重排完成后,原序列将变为下一个更大的排列next_permutation函数接收两个迭代器,表示序列的开始和结束位置。使用函数时,要求序列必须是允许重复元素的。函数会根据序列中的元素值进行重排。 函数使用步骤如下: 1. 首先需要包含<algorithm>头文件。 2. 调用next_permutation函数并传入序列的开始和结束迭代器。 3. 函数会返回一个布尔值,表示是否存在下一个更大的排列。 4. 如果返回true,表示存在下一个更大的排列,可以通过迭代器遍历并打印出来。 5. 如果返回false,表示已经是最大排列,序列不再改变。 下面是一个示例代码段,展示了如何使用next_permutation函数: ```cpp #include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> nums = {1, 2, 3}; // 打印初始排列 for (int num : nums) { std::cout << num << " "; } std::cout << std::endl; // 获取下一个更大的排列 while (std::next_permutation(nums.begin(), nums.end())) { // 打印下一个更大的排列 for (int num : nums) { std::cout << num << " "; } std::cout << std::endl; } return 0; } ``` 运行以上代码,会输出以下结果: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 可以看到,函数通过改变序列中元素的排列顺序,依次生成了下一个更大的排列。 ### 回答3: next_permutation函数是C++中的一个算法函数,用于获取给定排列的下一个排列。该函数接受两个迭代器作为参数,并在原地将排列重新排列为下一个排列。如果存在下一个排列,则该函数返回true,否则返回false。 next_permutation函数的算法原理是,从后往前找到第一个非递增的元素,记为i。然后再从后往前找到第一个大于i的元素,记为j。将i和j交换位置,并将i之后的元素按照升序重新排列,即得到下一个排列。 下面以一个例子来说明next_permutation函数使用。假设有一个排列{1, 2, 3},我们使用next_permutation函数来获取所有的排列。 首先,初始排列是{1, 2, 3},然后调用next_permutation函数,它会返回true,并将排列修改为{1, 3, 2}。接着再次调用next_permutation函数,会得到{2, 1, 3},再次调用则得到{2, 3, 1},继续调用则得到{3, 1, 2},最后再次调用则得到{3, 2, 1},此时函数返回false,表示已经获取到了所有的排列。 总结来说,next_permutation函数是用于获取给定排列的下一个排列函数,通过不断调用该函数,可以获取排列集合中的所有排列

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值