C++中有很多库函数,作用就是为了编程的方便,以前学习c++的时候并不知道什么是标准库,只是在每次编程的时候要用到#include<iostream>和using namespace std;是C++中的标准输入输出,偶尔做过一道机试题目——后缀字串排序,看到了标准库模板里面的algorithm,之前自己都是变一系列排序算法,但是用了algorithm之后,才发现标准库模板是如此好用,对快速开发相当有用处。
![得意](http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif)
下面就说一下algorithm标准库模板吧:
algorithm是用来解决排序、逆序等算法的库函数,其中包括了很多函数,比如sort(start,end,cmp);其中start以及end均指向待排序数组的首地址以及末地址,cmp是自己编的排序算法,可是是升序,也可以是降序,这个是自己的情况而定。
再如升序函数:next_pemutation(arr,arr+N)、降序函数:prev_pemutation(arr,arr+N);
下面通过具体的事例介绍这两个函数:
#include<iostream>
#include<algorithm> using namespace std;
const int N = 4; int arr[N] = {1,2,3,4};
int main() { do{ for(int i=0; i < N; i++) printf("%d ",arr[i]); putchar('\n'); }while(next_permutation(arr,arr+N));//prev_permutation(arr,arr+N) 的话arr里的数据按降序排列
return 0; }