一、栈stack
包含头文件:stack
定义: stack<int> name; //int型的
用法:
出栈: name.pop(); //出栈
入栈: name.push(T); //T为入栈元素
栈顶: name.top() //返回栈首
判空: name.empty(); //若为空返回true
长度: name.size(); //栈的大小
二、队列queue
包含头文件:queue
定义: queue<int> name; //int型的
用法:
出队: name.pop();
入队: name.push(T); //T为入栈元素
队首: name.front()
判空: name.empty() //若为空返回true
队尾: name.back()
长度: name.size();
三、优先队列priority_queue
包含头文件:queue
struct node
{
friend bool operator < (const node a,const node b)
{
return a.priority < b. priority;
}
int priority; //优先级
int value; //值
};
定义: priority_queue<node,vector<node>,cmp> name;
用法:
队首元素:name.top()
其余操作和队列一样
四、堆 heap
包含头文件:algorithm
STL里面的堆操作一般用到的只有4个:make_heap();、pop_heap();、push_heap();、sort_heap();
1、make_heap();
函数原型:void make_heap(first_pointer,end_pointer,compare_function);
一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字。在缺省的时候,默认是大跟堆。(下面的参数都一样就不解释了)
作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)
2、pop_heap();
函数原型:void pop_heap(first_pointer,end_pointer,compare_function);
作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它把first和last交换,然后将[first,last-1)的数据再做成一个堆。
3、push_heap();
函数原型:void pushheap(first_pointer,end_pointer,compare_function);
作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加进来,做成一个堆。
4、sort_heap()
函数原型:void sort_heap(first_pointer,end_pointer,compare_function);
作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然,经过排序之后就不是一个有效堆了)
下面是例子:
五、快速排序sort
包含头文件:algorithm
用法:
sort(a,a+n); //由小到大
sort(a,a+n,cmp); //cmp排序的比较函数
qsort( a , SIZE , sizeof(a[0])*SIZE , cmp);
cmp写法
1、int/char型
2、double型
3、char * 型
4、结构体一级排序
5、结构体二级排序
六、稳定快速排序stable_sort
包含头文件:algorithm
用法:
stable_sort (a,a+n); //由小到大
stable_sort (a,a+n,cmp); //cmp排序的比较函数
七、第K小数nth_element
包含头文件:algorithm
用法:nth_element(c,c+z,c+j);
解释:从c开始到c+j的数组中第K小元素,数组c第K个即第K小数,c[K-1]即为第K小数,且之前的都比这个数小。
八、KMP算法
KMP算法