首页 > 其他 > 详细

next_permutation & prev_permutation

时间:2015-03-31 09:01:32      阅读:244      评论:0      收藏:0      [点我收藏+]
#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

原文:http://blog.csdn.net/u012925008/article/details/44763463

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!