STL/next_permutation()和prev_permutation()函数
next_permutation()和prev_permutation()函数的使用:
next_permutation()和prev_permutation()函数的头文件添加为:#include <algorithm>
算法:
首先,从最为段开始往前寻找两个相邻的元素,令第一个元素索引为 endi 第二个元素索引为 endii ,且满足 array[endi]<array[endii] 。然后,再从最尾端开始向前检测,找到第一个大于 array[endi] 的元素,令其为索引 j 。将元素 array[endi],array[j] 对调,然后将 endii 之后的所有元素颠倒排列。即求的下一个排列。
解释:
一、如果数组 k 以后的是一个递减序列,则仅依靠调换 k 以后的元素不可能完成任务,所以必须找到满足 k>k+1 的元素,即保证 k 以后的序列不递减。
二、满足一之后,那么下一个序列的第 k 位一定是从后面找到刚好比 a[k] 大的一个比 a[k] 大的一个数打头的(为了保证刚好大于,又 k+1 之后的元素递减,所以从数组尾开始找到第一个比 a[k] 大的元素即可满足要求)。将这个数的索引记为 j 。
三、将 a[j] 与 a[k] 对调。此时, j 后面的元素是降序的。所以需要把 j 后面的逆转一下,从降序到升序,如此就得到了恰好比之前序列大一号的序列。
代码:next_permutation
bool nextPermutation(int array[],int len)
{
int endi=len-1;
int endii;
if(len==1)return false;
while(true)
{
endii=endi;
endi--;
if(array[endi]<array[endii])// 如果前一个元素小于后一个元素
{
int j=len;
while(array[--j]<array[endi]);// 由尾端往前找,直到遇上比array[endi]大的元素
swap(array[j],array[endi]); // 交换找到的元素
reverse(array+endii,array+len-1);// 将 endii 之后的元素全部逆向重排
return true;
}
if(endi==0)//排列已至最大,无下一个排列
{
reverse(array,array+len-1);
return false;
} }
}
代码:prev_permutation
bool prevPermutation(int array[],int len)
{
int endi=len-1;
int endii;
if(len==1)return false;
while(true)
{
endii=endi;
endi--;
if(array[endi]>array[endii])// 如果前一个元素大于后一个元素
{
int j=len;
while(array[--j]>array[endi]);// 由尾端往前找,直到遇上比array[endi]小的元素
swap(array[j],array[endi]); // 交换找到的元素
reverse(array+endii,array+len-1);// 将 endii 之后的元素全部逆向重排
return true;
}
if(endi==0)//排列已至最小,无上一个排列
{
reverse(array,array+len-1);
return false;
} }
}
next_permutation()和perv_permutation()函数的用法:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; //next_permutation()全排列的下一个
//prev_permutation()全排列的前一个,括号中都为数组的起始结束位置的指针 void print_int(int a[], int length) {//这个用来输出数组 for (int i = 0; i < length; i++) { cout << a[i] << " "; } cout << "\t";
} int main() { int t_int1[4] = {1, 2, 3, 4}, t_int2[4] = {4, 3, 2, 1}; string t_str1 = "abcd", t_str2 = "dcba"; cout << "use next_permutation:" << endl << endl; cout << "for int:" << endl << endl; print_int(t_int1, 4); while (next_permutation(t_int1, &t_int1[4])) {//当没有下一个时返回false print_int(t_int1, 4); } cout << endl; cout << endl << "for str:" << endl << endl; cout << t_str1 << " \t"; while (next_permutation(t_str1.begin(), t_str1.end())) { cout << t_str1 << " \t"; } cout << endl << endl; cout << "use prev_permutation:" << endl << endl; cout << "for int:" << endl << endl; print_int(t_int2, 4); while (prev_permutation(t_int2, &t_int2[4])) {//当没有前一个时返回false print_int(t_int1, 4); } cout << endl; cout << endl << "for str:" << endl << endl; cout << t_str2 << " \t"; while (prev_permutation(t_str2.begin(), t_str2.end())) { cout << t_str2 << " \t"; } cout << endl; return 0;
}
运行结果:
STL/next_permutation()和prev_permutation()函数相关推荐
- C++:全排列函数next_permutation()和prev_permutation()
文章目录 前言 一.next_permutation()用法 简单使用 例子1 第m个最小的数字序列 自定义排序 大小写字母排序 二.prev_permutation()用法 三.STL next_p ...
- 【C++】next_permutation / prev_permutation函数
关于next_permutation函数 next_permutation和prev_permutation函数都是C++STL中的全排列函数. 函数原型: #include < algorit ...
- C++ STL 容器元素排列之next_permutation和prev_permutation的使用示范
//本程序示例演示next_permutation和prev_permutation #include <iostream> #include <vector> #includ ...
- prev_permutation函数
prev_permutation函数是与next_permutation作用相反的函数. prev_permutation函数是生成给定序列的上一个较小的排列.
- next_permutation、prev_permutation以及is_permutation使用
next_permutation.prev_permutation以及is_permutation使用 next_permutation prev_permutation is_permutation ...
- C++STL中的unique函数解析
一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即"删除"序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的 ...
- c++STL中的find()函数 有两种使用方法
c++STL中的find()函数 有两种使用方法 方法一: 开头引头文件:中的函数 其调用形式为 find(start,end,value) start搜寻的起点,end搜寻的终点,要寻找的value ...
- STL里面的sort()函数使用结构体
当用sort()想对结构体进行排序的时候,这时候就需要自己写一个排序函数了 写这个是因为自己好久没用STL里面的sort()函数了.都忘记了 .再次实现一下 其实排序函数也很简单,自己实现 代码如下: ...
- c++ hashset的用法_c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
最新文章
- 画布canvas标签,并且在画布上画简单的形状
- JS作用域理解(声明提升)
- 1.5 Python基础知识 - while循环
- HttpClient实现客户端与服务器的通信
- python获取系统参数_python 常用系统参数
- Tensorflow 报错:tensorflow.python.framework.errors_impl.InternalError: Failed to create session....
- 【Vegas原创】如何配置 SQL Server 2005 以允许远程连接
- CentOS 7 配置DHCP服务器
- 同轴全息matlab仿真,基于MATLAB的计算全息干涉图仿真
- 如何在Linux上下载MongoDB
- 关于安卓脚本打包apk
- Android开发方向,我们如何选择?
- 呷哺呷哺的中年危机与贺光启的囚徒困境
- python绘制网格线在原图上面_在pyqtgraph中在图像上显示网格线
- alios下载_AliOS Studio(阿里编程工具)
- Python根据字幕文件自动给视频添加字幕(通用版)
- php webshell 分析,一款奇葩的PHP Webshell后门分析
- Android-适配各国语言、屏幕尺寸、系统版本及常见适配方法总结
- WMS系统4.0,仓库管理的20年历史变局你知道吗?
- java实现多线程爬取表情包
热门文章
- 下载Android Wear助手,fitwear下载
- C#如何实现窗体控件大小随窗体大小变化(包括字体)
- App Store生存:iOS开发者经验分享
- python中怎么绘制柱状簇_matplotlib中的簇状条形图?
- 如何渲染一个生物 - Minecraft 1.16 Forge Mod 开发笔记
- Keepalived与HaProxy的协调合作原理分析
- 你真的懂面形误差PV和RMS的计算方法吗?均方根(RMS)与方差、标准差有什么区别?Zemax中的波前RMS是什么?(光学测量、光学设计必看)
- 基于Servlet的图书商城系统-一天完成毕业设计,就这么简单
- 【算法设计zxd】第3章 迭代法 杨辉三角,穿越沙漠,内存移动,竖式相乘(阶乘)
- Android开源项目(一)音乐播放器