文章作者:姜南(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 ;
}

其中还用到了 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 ;
}

转载请注明 使用STL的next_permutation函数生成全排列(C++)

使用STL的next_permutation函数的更多相关文章

  1. c++中STL中的next_permutation函数基本用法

    对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...

  2. stl算法:next_permutation剖析

    在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组数据的全排列.但是怎么用,原理如何,我做了简单的剖析. 首先查看stl中相关信息.函数原型: templa ...

  3. 全排列函数 nyoj 366(next_permutation()函数)

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  4. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

  5. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  6. STL区间成员函数及区间算法总结

    STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间 ...

  7. POJ-1256 next_permutation函数应用

    字典序列: 在字典序中蕴含着一个点,就是大小的问题,谁先出现,谁后出现的问题.譬如a<b<c,出现顺序就是a,b,c. 本题中字符集是所有大小写字母,而题目中规定的谁大谁小已经不是按asc ...

  8. STL 算法中函数对象和谓词

    STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...

  9. 【模板】全排列(运用STL的next_permutation)

    (1) 先将要排列的数据存入数组中: (2) 再将数组元素从小到大排序: (3) 每次调用next_permutation函数,都只进行1次排列,若数组元素完全变为递减的排列,则该函数返回0: int ...

随机推荐

  1. lua --- __newindex 的使用规则

    1.如果 __newindex 是一个函数,在给 table 不存在的字段赋值的时候,会调用这个函数2.如果 __newindex 是一个table,在给 table 不存在的字段赋值的时候,会直接给 ...

  2. CPU、OpenGL/DirectorX、显卡驱动和GPU之间的关系

  3. RabbitMQ消费方式汇总

    在学习本章节前,请先学习之前的章节:Java访问RabbitMQ:https://www.cnblogs.com/duanjt/p/10057330.htmlRabbitMQ消息发布时的权衡:http ...

  4. 从fasta中提取或者过滤掉多个序列

    Google了一下,现成的工具不多. 自己写代码也可以,就是速度肯定不快,而且每次写也很麻烦. 偶然看到QIIME的filter_fasta.py有这个功能,从name list中提取多个序列. fi ...

  5. 最长上升子序列 nlogn

    ; LL num[N]; LL dp[N]; LL go(LL l, LL r, LL k) { for (; r >= l; r--) if (dp[r] <= k) return r; ...

  6. android studio 中的快捷键汇总

    1.Ctrl + B 跳入/跳出方法或者资源文件.将鼠标光标定位到某个方法或者资源id的调用处,按Ctrl + B,将会跳入该方法或者资源文件内部,功能等同于Ctrl + 鼠标左键.如果将鼠标光标定位 ...

  7. win10系统同时安装python2和python3

    1.官网下载python2和python3版本 2.安装python3,勾上Add Python3.5 to PATH,自定义选择安装目录,安装,验证:WIN+R--->cmd,输入python ...

  8. scrapy 爬虫框架之持久化存储

    scrapy  持久化存储 一.主要过程: 以爬取校花网为例 : http://www.xiaohuar.com/hua/ 1.  spider    回调函数     返回item 时    要用y ...

  9. 微信小程序选择视频,视频上传,视频播放

    请查看链接地址看具体详情: 选择视频: https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-video.html#wxchoosevideoobje ...

  10. mysqli的使用

    <?php /** 数据库连接 **/ $conn=mysqli_connect('localhost:3306','root','root'); if(!$conn){ die("c ...