码迷,mamicode.com
首页 > 其他好文 > 详细

next_permutation & prev_permutation

时间:2015-03-31 09:04:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!