C++——全排列函数next_permutation和prev_permutation

本文详细介绍了C++中`next_permutation`和`prev_permutation`函数的使用,包括函数原型、参数解释及实例演示。这两个函数分别用于生成序列的下一个和上一个字典序排列,可用于实现全排列遍历。示例代码展示了如何在整数数组和字符串中应用这两个函数,并提供了自定义比较函数的例子。
摘要由CSDN通过智能技术生成

 


 
 

next_permutation函数

  next_permutation 函数包含在头文件< algorithm >中。
  next_permutation 函数用于将范围 [first, last) 中的元素重新排序为下一个字典序更大的排列。排列种数:N!。N = 范围内的元素个数。
  默认使用运算符 < 比较元素,或者使用给定的二进制比较函数 comp 比较元素。

 
 

函数原型

template <class BidirectionalIterator>
bool next_permutation (BidirectionalIterator first, BidirectionalIterator last);

template <class BidirectionalIterator, class Compare>
bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); 

参数:

  • first:一个双向迭代器,指向要排列的范围内的第一个元素。
  • last:一个双向迭代器,指向要排列的范围内的最后一个元素的后一个位置。
  • comp:用户自定义的比较函数,它接受两个参数,如果两个参数按顺序返回真,否则返回假。它遵循严格的弱排序来对元素进行排序。

返回值:

  • 如果函数可以将对象重新排序为字典序更大的排列,则返回 true。
  • 否则,该函数返回 false 以指示排列不大于先前的排列,而是可能的最低排列(按升序排序)。

 
 

使用

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool comp(char a, char b){
	return a > b;
}

int main(){
	int num[] = {1, 2, 3};
	do{
		for(int i = 0; i < 3; i ++)
			printf("%d ", num[i]);
		printf("\n");
	}while(next_permutation(num, num + 3));
	printf("最后数组的排列:%d %d %d\n\n", num[0], num[1], num[2]);
	
	int num1[] = {2, 1, 3};
	do{
		for(int i = 0; i < 3; i ++)
			printf("%d ", num1[i]);
		printf("\n");
	}while(next_permutation(num1, num1 + 3));
	printf("最后数组的排列:%d %d %d\n\n", num1[0], num1[1], num1[2]);
	
	string str = "abb";
	do{
		cout << str << endl;
	}while(next_permutation(str.begin(), str.end()));
	
	str = "cba";
	printf("\n\n");
	do{
		cout << str << endl;
	}while(next_permutation(str.begin(), str.end(), comp));//自定义排序规则 
	
	return 0;
}

运行结果

 
 

 
 

 
 

prev_permutation函数

  prev_permutation 函数包含在头文件< algorithm >中。
  prev_permutation 函数用于将 [first, last) 范围内的元素重新排序为之前的字典序排列。就是和next_permutation函数相反的方向排列。排列种数:N!。N = 范围内的元素个数。
  默认使用运算符 < 比较元素,或者使用给定的二进制比较函数 comp 比较元素。

 
 

函数原型

template <class BidirectionalIterator>
bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last);

template <class BidirectionalIterator, class Compare>
bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); 

参数:

  • first:一个双向迭代器,指向要排列的范围内的第一个元素。
  • last:一个双向迭代器,指向要排列的范围内的最后一个元素的后一个位置。
  • comp:用户自定义的比较函数,它接受两个参数,如果两个参数按顺序返回真,否则返回假。它遵循严格的弱排序来对元素进行排序。

返回值:

  • 如果函数可以将对象重新排序为字典序更小的排列,则返回 true。
  • 否则,该函数返回 false 以指示排列不小于先前的排列,而是可能的最大排列(按降序排序)。

 
 

使用

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool comp(char a, char b){
	return a < b;
}

int main(){
	int num[] = {3, 2, 1};
	do{
		for(int i = 0; i < 3; i ++)
			printf("%d ", num[i]);
		printf("\n");
	}while(prev_permutation(num, num + 3));
	printf("最后数组的排列:%d %d %d\n\n", num[0], num[1], num[2]);
	
	int num1[] = {2, 1, 3};
	do{
		for(int i = 0; i < 3; i ++)
			printf("%d ", num1[i]);
		printf("\n");
	}while(prev_permutation(num1, num1 + 3));
	printf("最后数组的排列:%d %d %d\n\n", num1[0], num1[1], num1[2]);
	
	string str = "bba";
	do{
		cout << str << endl;
	}while(prev_permutation(str.begin(), str.end()));
	
	str = "cba";
	printf("\n\n");
	do{
		cout << str << endl;
	}while(prev_permutation(str.begin(), str.end(), comp));//自定义排序规则 
	
	return 0;
}

运行结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值