C++中的全排列函数next_permutation和prev_permutation

全排列
涉及两个函数:
prev_permutation (1)
next_permutation (2)

#include<algorithm>
//特别注意函数参数是迭代器类型,使用时不要写错
bool prev_permutation(iterator begin,iterator end);
bool next_permutation(iterator begin,iterator end);

这两个函数的功能一样,都是执行全排列。但是区别在于(1)求的是排列的上一个排列,而(2)求的是排列的下一个排列。这样说可能有点不理解,下面我用几个例子说明一下。
prev_permutation

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

class Solution1 {
public:
	vector<vector<int>> rest;
	//全排列数组
	void Permutation(int length) {
		vector<int> Ori;
		for (int i = length; i > 0; i--)Ori.push_back(i);
		cout << "数组中的数字为:" << endl;
		for (int i : Ori)cout << i << " ";
		cout << endl;
		cout << "全排列结果为:" << endl;
		do {
			vector<int> Temp;
			for (int t : Ori)Temp.push_back(t);
			rest.push_back(Temp);
		} while (prev_permutation(Ori.begin(), Ori.end()));
	}
};

int main() {
	Solution1 s1;
	vector<vector<int>> vec2;
	s1.Permutation(3);
	vec2 = s1.rest;
	for (int i = 0; i < vec2.size(); i++) {
		for (int j = 0; j < vec2[i].size(); j++) {
			cout << vec2[i][j] << " ";
		}
		cout << endl;
	return 0;
}

在这里插入图片描述
同样的,上个例子改为使用函数next_permutation

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

class Solution1 {
public:
	vector<vector<int>> rest;
	//全排列数组
	void Permutation(int length) {
		vector<int> Ori;
		for (int i = length; i > 0; i--)Ori.push_back(i);
		cout << "数组中的数字为:" << endl;
		for (int i : Ori)cout << i << " ";
		cout << endl;
		cout << "全排列结果为:" << endl;
		do {
			vector<int> Temp;
			for (int t : Ori)Temp.push_back(t);
			rest.push_back(Temp);
		} while (next_permutation(Ori.begin(), Ori.end()));
	}
};

int main() {
	Solution1 s1;
	vector<vector<int>> vec2;
	s1.Permutation(3);
	vec2 = s1.rest;
	for (int i = 0; i < vec2.size(); i++) {
		for (int j = 0; j < vec2[i].size(); j++) {
			cout << vec2[i][j] << " ";
		}
		cout << endl;
	return 0;
}

在这里插入图片描述
对于这个结果肯定不是我截图没截全,是确实就这么多,可能原理还不是很清楚,再换个例子试试,这次数组内元素反过来
prev_permutation

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

class Solution1 {
public:
	vector<vector<int>> rest;
	//全排列数组
	void Permutation(int length) {
		vector<int> Ori;
		for (int i = 0; i < length; i++)Ori.push_back(i);
		cout << "数组中的数字为:" << endl;
		for (int i : Ori)cout << i << " ";
		cout << endl;
		cout << "全排列结果为:" << endl;
		do {
			vector<int> Temp;
			for (int t : Ori)Temp.push_back(t);
			rest.push_back(Temp);
		} while (prev_permutation(Ori.begin(), Ori.end()));
	}
};

int main() {
	Solution1 s1;
	vector<vector<int>> vec2;
	s1.Permutation(3);
	vec2 = s1.rest;
	for (int i = 0; i < vec2.size(); i++) {
		for (int j = 0; j < vec2[i].size(); j++) {
			cout << vec2[i][j] << " ";
		}
		cout << endl;
	return 0;
}

在这里插入图片描述
这时换为函数next_permutation

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

class Solution1 {
public:
	vector<vector<int>> rest;
	//全排列数组
	void Permutation(int length) {
		vector<int> Ori;
		for (int i = 0; i < length; i++)Ori.push_back(i);
		cout << "数组中的数字为:" << endl;
		for (int i : Ori)cout << i << " ";
		cout << endl;
		cout << "全排列结果为:" << endl;
		do {
			vector<int> Temp;
			for (int t : Ori)Temp.push_back(t);
			rest.push_back(Temp);
		} while (next_permutation(Ori.begin(), Ori.end()));
	}
};

int main() {
	Solution1 s1;
	vector<vector<int>> vec2;
	s1.Permutation(3);
	vec2 = s1.rest;
	for (int i = 0; i < vec2.size(); i++) {
		for (int j = 0; j < vec2[i].size(); j++) {
			cout << vec2[i][j] << " ";
		}
		cout << endl;
	return 0;
}

在这里插入图片描述
我想看到这里应该有点明白了吧!如果不明白没关系,我接下来还有一个例子,看完这个例子你肯定就明白了,也能验证你的猜想。
next_permutation

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

class Solution1 {
public:
	vector<vector<int>> rest;
	//全排列数组
	void Permutation(int length) {
		vector<int> Ori = {2,1,3};
		//for (int i = 0; i < length; i++)Ori.push_back(i);
		cout << "数组中的数字为:" << endl;
		for (int i : Ori)cout << i << " ";
		cout << endl;
		cout << "全排列结果为:" << endl;
		do {
			vector<int> Temp;
			for (int t : Ori)Temp.push_back(t);
			rest.push_back(Temp);
		} while (next_permutation(Ori.begin(), Ori.end()));
	}
};

int main() {
	Solution1 s1;
	vector<vector<int>> vec2;
	s1.Permutation(3);
	vec2 = s1.rest;
	for (int i = 0; i < vec2.size(); i++) {
		for (int j = 0; j < vec2[i].size(); j++) {
			cout << vec2[i][j] << " ";
		}
		cout << endl;
	return 0;
}

在这里插入图片描述
prev_permutation

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

class Solution1 {
public:
	vector<vector<int>> rest;
	//全排列数组
	void Permutation(int length) {
		vector<int> Ori = {2,1,3};
		//for (int i = 0; i < length; i++)Ori.push_back(i);
		cout << "数组中的数字为:" << endl;
		for (int i : Ori)cout << i << " ";
		cout << endl;
		cout << "全排列结果为:" << endl;
		do {
			vector<int> Temp;
			for (int t : Ori)Temp.push_back(t);
			rest.push_back(Temp);
		} while (pre_permutation(Ori.begin(), Ori.end()));
	}
};

int main() {
	Solution1 s1;
	vector<vector<int>> vec2;
	s1.Permutation(3);
	vec2 = s1.rest;
	for (int i = 0; i < vec2.size(); i++) {
		for (int j = 0; j < vec2[i].size(); j++) {
			cout << vec2[i][j] << " ";
		}
		cout << endl;
	return 0;
}

在这里插入图片描述
这两次数组内的元素是乱序的,不像之前那样是从大到小或者是从小到大排列,然后(1)和(2)两个函数的结果加起来就是整个的全排列。
说明在数组乱序情况下,需要使用两个函数的加和再除去重复的数组元素才能得到整个数组的全排列。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页