# 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);


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;
}


#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;
}


#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;
}


03-29 1万+

07-26 473
02-16 143
03-05 286
01-18 121
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客