使用STL的next_permutation函数
文章作者:姜南(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函数的更多相关文章
- c++中STL中的next_permutation函数基本用法
对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...
- stl算法:next_permutation剖析
在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组数据的全排列.但是怎么用,原理如何,我做了简单的剖析. 首先查看stl中相关信息.函数原型: templa ...
- 全排列函数 nyoj 366(next_permutation()函数)
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- 3.2 STL中的函数对象类模板
*: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...
- 使用STL库sort函数对vector进行排序
使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...
- STL区间成员函数及区间算法总结
STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间 ...
- POJ-1256 next_permutation函数应用
字典序列: 在字典序中蕴含着一个点,就是大小的问题,谁先出现,谁后出现的问题.譬如a<b<c,出现顺序就是a,b,c. 本题中字符集是所有大小写字母,而题目中规定的谁大谁小已经不是按asc ...
- STL 算法中函数对象和谓词
STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...
- 【模板】全排列(运用STL的next_permutation)
(1) 先将要排列的数据存入数组中: (2) 再将数组元素从小到大排序: (3) 每次调用next_permutation函数,都只进行1次排列,若数组元素完全变为递减的排列,则该函数返回0: int ...
随机推荐
- Lua和C++交互 学习记录之六:全局函数交互
主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3 参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...
- 最新省市区数据,sql插入语句
--省数据 insert into Province (ProvinceName) values('北京市'); insert into Province (ProvinceName) value ...
- makefile 里的 := , = , +=
:= 是在这行代码的时候,直接展开右边的变量. = 是在最终左边变量被使用的时候,才把右边的变量展开. https://stackoverflow.com/questions/10227598/wha ...
- MySQL official tutorial
1.installation 2.setup environment variables add %/MySQL Server/bin to path. then restart cmd/powers ...
- apiCloud 三方分享,微信好友分享,微信朋友圈分享,QQ分享,微博分享
首先查看我的这篇有关三方登录的博客,地址是http://www.cnblogs.com/gqx-html/p/8303567.html,配置完三方数据后可以从上一篇文章中的链接跳转到各个登录查看api ...
- php 常用函数总结
1. is_numeric() // 判断变量是不是数字或者数字字符串 2. array_column() // 获取某一列 3. array_search() // array_search(val ...
- canal入门Demo
关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...
- 第一阶段——站立会议总结DAY09
1.昨天做了什么:未做. 2.今天准备做什么:准备将之前讲的东西,要付诸实践.所以,为了使界面更加耐看,向微信,QQ这样的看齐,查一查个人中心界面中间的条条框框的实现代码,借鉴,并运用到自己的代码上. ...
- 【其他】【PL/SQL Developer】【1】解决PL/SQL Developer过期的情况
正文: 1,开始菜单,搜索regedit,回车打开(即日常搜索电脑安装的软件的地方,regedit就是注册表) 2,按HKEY_CURRENT_USER\Software\Allround Autom ...
- CSS Hack 技巧
IE Hack IE系列浏览器的hack大略如下: _nowamagic:1px;———–ie6 *nowamagic:1px;———–ie7 nowamagic:1px\0;———-ie89 now ...