algorithm是stl中最重要的头文件之一,其中定义了多种算法可供使用者直接调用。
#include <algorithm>
using namespace std;
1.max()、min()
分别返回两个数的最大值和最小值,参数可以是整数、浮点数,但必须是2个参数。
2.abs()
返回一个数的绝对值,参数必须是整数。
3.swap()
swap(x,y)可以交换x和y的值
4.next_permutation()
给出一个序列在全排列中的下一个序列
int a[10]={1,2,3};
next_permutation(a,a+3);
for(int i=0;i<3;i++)
printf("%d ",a[i]); //1 3 2
printf("\n");
next_permutation(a,a+3);
for(int i=0;i<3;i++)
printf("%d ",a[i]); //2 1 3
实例:输出一个序列的全排列
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int a[10]={1,2,3};
do{
printf("%d %d %d\n",a[0],a[1],a[2]);
}while(next_permutation(a,a+3)); //使用do while是为了输出第一个序列
return 0;
}
运行结果:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
5.reverse()
(1)反转数组元素
reverse(p1,p2),在数组指针[p1,p2)的范围内使元素反转
int a[5]={1,2,3,4,5};
int *p=&a[1];
reverse(p,p+3);
for(int i=0;i<5;i++)
printf("%d ",a[i]); //1 4 3 2 5
int a[5]={1,2,3,4,5};
reverse(a,a+3);
for(int i=0;i<5;i++)
printf("%d ",a[i]); //3 2 1 4 5
(2)反转容器元素
reverse(it1,it2),在迭代器[it1,it2)范围内使元素反转
vector<int> v;
for(int i=1;i<=5;i++)
v.push_back(i);
reverse(v.begin(),v.begin()+3);
for(vector<int>::iterator it=v.begin();it!=v.end();it++)
printf("%d ",*it); //3 2 1 4 5
6.fill()
fill(it1,it2,x),将数组或容器的某个区间[it1,it2)内填充相同的值x
//数组
int a[10]={1,2,3,4,5};
fill(a,a+3,0);
for(int i=0;i<5;i++)
printf("%d ",a[i]); //0 0 0 4 5
//容器
string str="dogdogaaa";
fill(str.begin()+6,str.end(),'b');
for(string::iterator it=str.begin();it!=str.end();it++)
printf("%c",*it); //dogdogbbb
7.sort()
(1)数组元素排序
sort(p1,p2,cmp),p1和p2分别是排序区间首元素的地址和尾元素地址的下一个地址(左闭右开)。cmp是比较函数,可以不写,不写的话默认按升序排序。
int a[10]={1,4,3,2,5};
int *p=&a[1];
sort(p,p+3);
for(int i=0;i<5;i++)
printf("%d ",a[i]); //1 2 3 4 5
int a[10]={1,4,3,2,5};
sort(a,a+4);
for(int i=0;i<5;i++)
printf("%d ",a[i]); //1 2 3 4 5
如果想要降序排序可以使用比较函数cmp:
#include <stdio.h>
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
return a>b; //可以理解为当a>b时a排在b的前面
}
int main(){
int a[10]={1,3,2,4,5};
sort(a,a+5,cmp);
for(int i=0;i<5;i++)
printf("%d ",a[i]); //5 4 3 2 1
return 0;
}
(2)容器排序
stl标准容器中,set和map元素本身是有序的,不能使用sort。可以使用sort排序的容器有vector、string和deque。
//vector排序
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
bool cmp(int a,int b){
return a>b; //vector中的元素是int类型,因此a,b也是int类型比较
}
int main(){
vector<int> v;
for(int i=1;i<=5;i++)
v.push_back(i);
sort(v.begin(),v.end(),cmp);
for(vector<int>::iteratorit=v.begin();it!=v.end();it++)
printf("%d ",*it); //5 4 3 2 1
return 0;
}
//string排序
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
string str="edcba";
int i=str.find("d");
sort(str.begin()+i,str.end());
cout<<str<<endl; //eabcd
return 0;
}
(3)结构体数组排序
结构体之间本身并无大小关系,因此想要对结构体数组进行sort排序就需要通过cmp函数制定比较规则。
假设定义了如下结构体数组:
struct node{
int x,y;
}n[100];
只对元素x进行升序排序的cmp函数:
bool cmp(node a,node b){
return a.x<b.x;
}
先对元素x进行升序排序,如果x相等,再对y进行降序排序的cmp函数:
bool cmp(node a,node b){
if(a.x!=b.x)
return a.x<b.x;
else
return a.y>b.y;
}
一个简单实例:
#include <stdio.h>
#include <algorithm>
#include <string>
using namespace std;
struct node{
int x,y;
}n[100];
bool cmp(node a,node b){
if(a.x!=b.x)
return a.x<b.x;
else
return a.y>b.y;
}
int main(){
n[0].x=1;
n[0].y=3;
n[1].x=1;
n[1].y=4;
n[2].x=2;
n[2].y=5;
sort(n,n+3,cmp);
for(int i=0;i<3;i++)
printf("%d %d\n",n[i].x,n[i].y);
return 0;
}
运行结果:
1 4
1 3
2 5
8.lower_bound()、upper_bound()
lower_bound(begin,end,value)可以寻找一个有序数组或容器中[begin,end)范围内第一个值大于等于value的元素,数组返回该位置指针,容器返回迭代器。upper_bound(begin,end,value)寻找的是第一个值大于value的元素,其余同上。
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
vector<int> v;
for(int i=1;i<=10;i++)
v.push_back(i);
vector<int>::iterator it1=lower_bound(v.begin()+2,v.end(),3);
vector<int>::iterator it2=upper_bound(v.begin()+2,v.end(),3);
printf("%d %d\n",*it1,*it2); //3 4
return 0;
}