标签:algorithm
#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; }
next_permutation & prev_permutation
标签:algorithm
原文地址:http://blog.csdn.net/u012925008/article/details/44763463