next_permutation全排列算法

C++ STL中的一个可以方便的生成全排列的算法:

#include <algorithm>
bool next_permutation( iterator start, iterator end );

使用举例:

#include <iostream>
#include <algorithm>
#include <string>
 
using namespace std;
 
int main()
{
    string str;
    cin >> str;
    sort(str.begin(), str.end());//必须进行这一步才可以
    cout << str << endl;
    while (next_permutation(str.begin(), str.end()))
        cout << str << endl;
    return 0;
}

qsort(数组名,数组长度,数组中每个元素大小,compare);     compare函数的写法决定了排序是升序还是降序。需要#include<stdlib.h>
例如:
int compare(const void*a,const void*b){return *(*int)a-*(int*)b;}  
示例:qsort(a,10,sizeof(int),compare)    //假设已定义了整型数组a[10]
升序排序的写法,如果是:return *(*int)b-*(int*)a   就是降序,不论是什么类型的数组,该函数的形式都是这样的
sort(数组名,数组末地址,compare)    //若不写compare则默认升序排列,需要#include<algorithm>
例如:
sort(a,a+10);        //将数组a以升序排序,假设先前定义了a[10]并输入了数据 
摘自百度知道……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值