algorithm头文件下的常用函数

algorithm头文件常用高效函数

  • max()

max(a, b)返回a和b中的最大值,参数必须是两个(可以是浮点型)。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a=3,b=6,c=8;    
 7     cout<<a<<" "<<b<<"最大值为"<<max(a,b)<<endl;
 8     cout<<a<<" "<<b<<" "<<c<<"最大值为"<<max(a,max(b,c))<<endl;    //可以嵌套使用
 9     return 0; 
10 } 
  • min()

基本用法同max(),不同的是min()求的是最小值。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a=3,b=6,c=8;    
 7     cout<<a<<" "<<b<<"最小值为"<<min(a,b)<<endl;
 8     cout<<a<<" "<<b<<" "<<c<<"最小值为"<<min(a,min(b,c))<<endl;    //可以嵌套使用
 9     return 0; 
10 } 
  • swap()

swap(a, b)用来交换a和b的值。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a=3,b=6;
 7     cout<<a<<" "<<b<<endl;    //交换前 
 8     swap(a,b);
 9     cout<<a<<" "<<b<<endl;    //交换后 
10     return 0; 
11 } 
输出结果:
3 6
6 3
  • reverse()

reverse(it1, it2)可以将数组指针在[it1, it2)之间的元素或容器的迭代器在[it1, it2)范围内的元素进行反转。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a[5] = {1,2,3,4,5};
 7     reverse(a,a+3);    //将a[0]-a[2]反转 
 8     for(int i=0;i<5;i++){
 9         cout<<a[i]<<" ";
10     }
11     return 0; 
12 } 
输出结果:
3 2 1 4 5
  • next_permutation()

next_permutation()用来求一个全排列的下一个序列。

例如,n=3的全排列

123
132
213
231
312
321

这样132的下一个序列就是213。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a[3] = {1,2,3};
 7     do{
 8         cout<<a[0]<<a[1]<<a[2]<<endl;
 9     }while(next_permutation(a,a+3));
10     return 0; 
11 } 
输出结果:
123
132
213
231
312
321
  • fill()

fill()可以把数组或容器中的某一段区间赋值为某个相同的值,与C语言中常用的memset不同的是,这里的赋值可以是数组类型对应范围中的任意值。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a[5] = {1,2,3,4,5};
 7     fill(a,a+5,6);    //将数组全部元素赋值为6
 8     for(int i=0;i<5;i++){
 9         cout<<a[i]<<" ";
10     } 
11     return 0; 
12 } 
输出结果:
6 6 6 6 6
  • sort()

sort()是常用的排序函数,效率较高,使用灵活方便。

使用sort函数的格式为

sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较函数(非必填));

sort函数默认为递增排序。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main() 
 5 {
 6     int a[8] = {5,3,2,1,4,9,8,0};
 7     sort(a,a+4);    //将a[0]-a[3]进行递增排序 
 8     for(int i=0;i<8;i++){
 9         cout<<a[i]<<" ";
10     } 
11     return 0; 
12 } 
输出结果:
1 2 3 5 4 9 8 0

相应的,sort函数还可以对其它类型的数组进行排序(如float、double、char),对char型数组排序时默认为字典序。

既然sort函数默认为递增排序,那我们如何实现递减排序呢?这时就需要用到我们的比较函数了。

实现比较函数

从上面的例子可以看出,如果sort函数中的比较函数不填,则默认按照递增排序,下面的例子我们使用比较函数来实现递减排序。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 bool cmp(int a,int b){  
 5     return a>b;
 6 } 
 7 int main() 
 8 {
 9     int a[8] = {5,3,2,1,4,9,8,0};
10     sort(a,a+4,cmp);    //将a[0]-a[3]进行递减排序 
11     for(int i=0;i<8;i++){
12         cout<<a[i]<<" ";
13     } 
14     return 0; 
15 } 
输出结果:
5 3 2 1 4 9 8 0

可以看出,通过添加了一个比较函数cmp(函数名字可以自行声明),我们实现了递减排序。cmp函数中return a>b规定了sort需要从大到小排序。相应的,如果写成return a<b,则从小到大排序。

algorithm头文件下还有很多方便我们编程的函数,这里只是举了几个平时写算法时经常用到的一些简易函数,也是算法竞赛中经常使用的几个函数。相信通过熟练使用这些函数,一定会提高我们编写算法的效率。

posted @ 2021-05-24 09:05  熊猫耳朵  阅读(318)  评论(0编辑  收藏  举报