#include< algorithm >是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模板函数。

常用的库函数:

一、max(),min()和swap()

  • max(x,y)  //返回两个元素中值最大的元素
  • min(x,y)  //返回两个元素中值最小的元素
  • swap(x,y)  //用来交换x和y的值

/*
使用algorithm头文件,需要在头文件下加一行using namespace std;”
*/ //常用函数max(), min(), abs()
//swap()
//reverse()
//next_permutation()
//fill()
// sort()
//lower_bound和upper_bound()/*max(), min(), abs()的使用*/
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;int main()
{int x =1, y =-2;cout <<max(x,y)<< " "<< min(x,y)<<endl;cout << abs(x)<<" "<<abs(y)<<endl;return 0;
} 

/*
swap()的使用
swap(x,y)用来交换x和y的值,示例如下:
*/#include<iostream>
#include<algorithm>
using namespace std;int main()
{int x=1, y=2, z;swap(x, y);cout<< x << " "<< y<<endl;swap(x, z); cout<<x<< z <<endl;swap(y, z); cout<<y<< z <<endl; return 0;
}

二、reverse()

  反转排序指定范围中的元素,reverse(a,b) 可以将数组指针在[a,b)之间的元素或容器的迭代器在[a,ib)范围内的元素进行反转。

/*reverse()的使用 */
/*reverse(it, it2)可以将数组指针在[it, it2)之间的元素或容器的迭代器在[it, it2)范围内的元素
进行反转。示例如下: *//*对整形数组逆转*/
#include<algorithm>
#include<iostream>using namespace std;int main()
{int a[10]= {10, 11, 12, 13, 14, 15}; reverse(a, a+4);//a[0]~a[3]反转for(int i=0; i<6; i++){cout<<a[i]<<" ";} return 0;
}/*对容器中的元素(例如string字符串)进行反转,结果也一样*/#include<iostream>
#include<string>
#include<algorithm>using namespace std;int main()
{string str = "abcdefghi";reverse(str.begin()+2, str.begin()+6);//对a[2]~a[5]逆转*左闭右开* for( int i=0; i < str.length(); i++){cout<<str[i]<<" ";}   return 0;
}

 
程序示例:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;int main()
{int a[10]={1,2,3,4,5,6,7,8,9,10};reverse(a,a+6);for(int i=0;i<10;i++){cout<<a[i]<<" ";}cout<<endl;string str="abcdefgh";reverse(str.begin()+2,str.begin()+6);for(int j=0;j<str.length();j++){cout<<str[j]<<" ";}cout<<endl;return 0;
}

运行结果:

三、fill()

  fill() 可以把数组或容器中的某一段区间赋为某个相同的值。

/*fill()函数 的使用*/
/*
fill()可以把数组或容器中的某一段区间赋为某个相同的值。和memset不同,这里的赋值可以是
数组类型对应范围中的任意值。示例如下:
*/ #include<iostream>
#include<string>
#include<algorithm>using namespace std;int main()
{int a[5] = {1,2,3,4,5};fill( a, a+5, 233);//将a[0]~a[4]赋值3为233 for(int i=0;i<5;i++){cout<<a[i]<<endl;}       return 0;
}

程序示例:

#include<iostream>
#include<algorithm>
using namespace std;int main()
{int a[5];fill(a,a+5,957);for(int i=0;i<5;i++){cout<<a[i]<<" ";}cout<<endl;return 0;
}

运行结果:

四 、 sort()

排序函数,默认为递增排序。
如果需要递减排序,需要增加一个比较函数:

bool cmp(int a,int b){return a>b;   //若a<b为递增排序
}
sort(a,a+n,cmp);
/*sort()函数的使用*/
//sort()使用 :sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较函数(非必填));
//如果不填写比较函数默认是递增数列#include<iostream>
#include<string>
#include<algorithm>using namespace std;int main()
{int a[6] = {9, 4, 2, 5, 6, -1};//将a[0]~a[3]从小到大排序sort(a, a+4);for( int i = 0; i<6; i++){cout<<*(a+i)<<" ";} cout<<" "<<endl;sort(a, a+6);//将a[0]~a[5]从小到大排序for( int i = 0; i<6; i++){cout<<*(a+i)<<" ";}return 0;
}

程序示例:

#include<iostream>
#include<algorithm>
using namespace std;bool cmp(int a,int b){return a>b;
}
int main()
{int i;int a[7]={8,6,9,2,7,4,3};cout<<"排序前数组为:";for(i=0;i<7;i++){cout<<a[i]<<" ";}cout<<endl;sort(a,a+7);cout<<"递增排后前数组为:";for(i=0;i<7;i++){cout<<a[i]<<" ";}cout<<endl;sort(a,a+7,cmp);cout<<"递减排后前数组为:";for(i=0;i<7;i++){cout<<a[i]<<" ";}cout<<endl;return 0;
}

运行结果:

五、next_permutation()

  返回给定范围中的元素组成的下一个按字典序的排列,即给出一个序列在全排列中的下一个序列。

/*next_permutation的用法
/*@注意!!!* 只有在是个有小到大的序列!!!!!*/
#include<iostream>
#include<string>
#include<algorithm>using namespace std;int main()
{int a[10] = {3, 2, 1};do{cout<<a[0]<< " "<<a[1]<<" "<<a[2]<<endl;    }while(next_permutation(a, a+3));return 0;
}

程序示例:

#include<iostream>
#include<algorithm>
using namespace std;int main()
{int a[5]={1,2,3};do{cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;}while(next_permutation(a,a+3));return 0;
}

运行结果:

六、lower_bound()和upper_bound()

  lower_bound 和 upper_bound()需要用在一个有序数组或容器中。
  lower_bound(first,last,val) 用来寻找在数组或容器的[first,last)范围内第一个值大于等于val元素的位置,如果是数组,返回该位置的指针;若果是容器,返回该位置的迭代器
  upper_bound(first,last,val) 用来寻找在数组或容器的[first,last)范围内第一个值大于val元素的位置,如果是数组,返回该位置的指针;若果是容器,返回该位置的迭代器

程序示例:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int a[10]={1,2,2,3,3,4,4,5,6,7};int *lowerPos=lower_bound(a,a+10,3);int *upperPos=upper_bound(a,a+10,3);cout<<"lower_bound(a,a+10,3)="<<lowerPos-a<<endl;cout<<"upper_bound(a,a+10,3)="<<upperPos-a<<endl;return 0;
}

其他的函数:

/*cmp函数的使用 */ /*(1)cmp对于整型数组*/
#include<iostream>
#include<string>
#include<algorithm>using namespace std;bool cmp(int a, int b)
{return a > b;  //可以理解为当a>b时把a放在b前面
}int main()
{int a[4] = {3, 1, 4,2}; sort(a,a+4,cmp);for(int i=0; i<4; i++){cout<<*(a+i)<<" ";}return 0;
}/*(2)同样对char型数组*/
#include<iostream>
#include<string>
#include<algorithm>using namespace std;bool cmp(char a, char b)
{return a > b;  //可以理解为当a>b时把a放在b前面
}int main()
{char a[4] = {'a', 'b', 'c','d'};    sort(a,a+4,cmp);for(int i=0; i<4; i++){cout<<*(a+i)<<" ";}return 0;
}/*(3)对结构体数组排序*/ /*定义结构体:
struct node{int x, y;
}ssd[10];
bool cmp(node a, node b)
{return a.x>b.x;//
}
*/
//示例如下:#include<iostream>
#include<algorithm>using namespace std;struct node{int x, y;
}ssd[10];bool cmp(node a,node b)
{return a.x > b.x; //按x值从大到小对结构体数组排序
}int main()
{ssd[0].x =2;ssd[0].y=2;ssd[1].x =1;ssd[1].y=3;ssd[2].x =3;ssd[2].y=1;sort(ssd, ssd+3, cmp);//排序 for( int i=0; i<3; i++){cout<< ssd[i].x <<endl;     } cout<<" "<<endl;return 0;
} /*lower_bound()和upper_bound()的使用 */
//lower_bound()和upper_bound() 需要用在一个有序数组或容器中/*
lower_bound(first, last, val)寻找数组或容器中[first, last)范围内第一个值大于等于val的元素位置,
如果是数组则返回该位置的指针;如果是容器则返回该位置的迭代器
lower_bound(first, last, val)寻找数组或容器中[first, last)范围内第一个值大于val的元素位置,
如果是数组则返回该位置的指针;如果是容器,则范围该位置的迭代器
**注意**
如果数组或容器中没有需要寻找的元素,则lower_bound()和upper_bound()均返回可以插入该元素的位置的指针或迭代器
(即假设存在该元素,该元素应当在的位置)
lower_bound()和upper_bound()的复杂度均为O(log(last-first))
*///示例如下: #include<iostream>
#include<algorithm>using namespace std;int main()
{int a[10]={1,2,2,3,3,3,5,5,5,5};//寻找-1 int* lowerPos = lower_bound(a, a+10, -1);int* upperPos = upper_bound(a, a+10, -1);cout<<lowerPos-a<< " "<<upperPos-a<< endl;  //寻找1lowerPos = lower_bound(a, a+10, 1);upperPos = upper_bound(a, a+10, 1);cout<<lowerPos-a<< " "<<upperPos-a<< endl;  //寻找2lowerPos = lower_bound(a, a+10, 2);upperPos = upper_bound(a, a+10, 2);cout<<lowerPos-a<< " "<<upperPos-a<< endl;  //寻找3lowerPos = lower_bound(a, a+10, 3);upperPos = upper_bound(a, a+10, 3);cout<<lowerPos-a<< " "<<upperPos-a<< endl;  //寻找4lowerPos = lower_bound(a, a+10, 4);upperPos = upper_bound(a, a+10, 4);cout<<lowerPos-a<< " "<<upperPos-a<< endl;  //寻找6lowerPos = lower_bound(a, a+10, 6);upperPos = upper_bound(a, a+10, 6);cout<<lowerPos-a<< " "<<upperPos-a<< endl;  return 0;
}

#include<algorithm>相关推荐

  1. C++ reverse函数的用法—头文件#include <algorithm>

    reverse函数功能是逆序(或反转),多用于字符串.数组.容器.头文件是#include reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指 ...

  2. 数据结构与算法(基于<algorithm>)

    algorithm算法库 一.排序算法(sort.stable_sort.partial_sort.nth_element) 1.代码示例 2.运行结果 二.查找算法(find.find_if.cou ...

  3. C++头文件 <algorithm>的 常用函数(详细)

    1.sort( ) 用于排序,默认从小到大排. 2.max( ):两数最大 3.min():两数最小 4.abs():求一个数的绝对值   (  与<cmath>中的fbs(),不同,因a ...

  4. c++ <algorithm> swap 函数 reverse 函数 remove 函数

    1. swap() swap(a,b) 2. reverse() reverse函数用于反转在[first,last)范围内的顺序,reverse函数没有返回值 例子:交换vector容器中元素的顺序 ...

  5. 详解C++中经常看到的#include <iostream> using namespace std;

    在cpp文件中,我们经常看到#include <iostream> using namespace std; 这两句连用.问题来了对于从C切换到C++的程序员而言为啥iostream 不写 ...

  6. C++【“using namespace std”的意思、#include “iostream“与#include < iostream>区别、< iostream>与< iostream.h>区别】

    目录 1."using namespace std"的意思 2.#include "iostream" 与 #include < iostream> ...

  7. C++ #include <.h>和“.h“的区别

    #include<.h>用于包含标准库 #include".h"用于包含自定义的头文件

  8. #include<set>头文件的用法

    #include<set>头文件的用法 转载与set的常见用法_hjl_heart的博客-CSDN博客_set用法

  9. #include <filename> 和 #include “filename“ 有什么区别?

    问: 在 include 指令中使用尖括号和引号有什么区别? #include <文件名> #include "文件名" 答1: huntsbot.com全球7大洲远程 ...

最新文章

  1. java query类是什么类_java – 从包生成QueryDsl Q类
  2. html5客户端本地存储之sessionStorage及storage事件
  3. hashlib 模块用来进行hash
  4. Hibernate隐藏的宝石:pooled-lo优化器
  5. 视频光端机各种视频接口的传输距离是多少?
  6. 瑟瑟发抖!拍照比“剪刀手”竟会泄露指纹信息...
  7. SLAM--G2o实现BA优化
  8. rvm、Ruby安装(亲测有效)
  9. 计算机类公务员提升空间,本人在公务员省考裸考申论61分,在之后还有多大的提升空间?...
  10. IP协议(IP协议报头、MTU、网段划分、NAT技术、路由的工作过程)
  11. 学习Nisy作者c语言教程笔记1
  12. html5文本框获取焦点,CSS3实现文本输入框获取焦点高亮显示
  13. 关于U盘病毒(又名Autorun病毒)
  14. ARM64+树莓派4b+JLINK仿真器实验环境搭建指南
  15. Progression Approximation---以无穷数列求和为例
  16. Vue:自定义组件引入单页面+动态绑定图片
  17. 【Git】Git命令大全
  18. 微信小程序(优惠券)
  19. python数据分析及可视化(一)课程介绍以及统计学的应用、介绍、分类、基本概念及描述性统计
  20. 常见的信号平滑处理方法

热门文章

  1. ArcGIS智能标注入门及基础实操
  2. 在鼠标点击特效上加上鼠标移动特效 (二)
  3. 【目标检测论文阅读笔记】Feature-Enhanced CenterNet for Small Object Detection in Remote Sensing Images
  4. amd可以python_python amd64什么意思
  5. python unicode-escape
  6. Android MMKV使用及 MMAP原理
  7. BLEUnlock ,解锁mac新方式!通过蓝牙解锁Mac电脑!
  8. 从Noke蓝牙挂锁,看经不住现实考验的先进技术
  9. Android开发————简易备忘录(三)
  10. 红外对管识别及红外测距系统原理