C++·头文件<algorithm>

本博文仅示例一些常用的函数:

  sort、for_each、

1. sort

/*
	STL - <algorithm> - sort
	template< class RandomIt, class Compare >
	  void sort( RandomIt first, RandomIt last, Compare comp );
			Eg:sort(array,array+10,bool cmpFunc)
	template< class RandomIt >
	  void sort( RandomIt first, RandomIt last );
			Eg:sort(vector.begin(),vector.end(),bool cmpFunc)
*/
#include <algorithm>
#include <iostream>
using namespace std;  

bool com(int a,int b) {
//	return a>b; //降序
	return a<b;//升序
}  

int main(){
	int a[10]={9,6,3,8,5,2,7,4,1,0};
	for(int i=0;i<10;i++)
		cout<<a[i]<<"\t";
	cout<<endl;
	sort(a,a+10,com);//com函数作为可选(自定义)的传入参数
 	for(int i=0;i<10;i++)
 		cout<<a[i]<<"\t";
 	return 0;
}
/*
9       6       3       8       5       2       7       4       1       0
0       1       2       3       4       5       6       7       8       9
*/

2.for_each

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;  

template<class T>
struct plus2
{
    void operator()(T&x)const
    {
        x+=2;
    }  

};  

void printElem(int& elem)
{
  cout << elem << endl;
}  

int main()
{
    int ia[]={0,1,2,3,4,5,6};
    for_each(ia,ia+7,printElem);//输出  

    int ib[]={7,8,9,10,11,12,13};
    vector<int> iv(ib,ib+7);
    for_each(iv.begin(),iv.end(),plus2<int>());//更改元素
    for_each(iv.begin(),iv.end(),printElem);//输出
    return 0;
}

 

3.其他常用函数

  移步:[头文件algorithm中的常用函数](https://www.cnblogs.com/TWS-YIFEI/p/5813256.html) 

原文地址:https://www.cnblogs.com/johnnyzen/p/9095708.html

时间: 2024-08-29 05:13:04

C++·头文件<algorithm>的相关文章

头文件algorithm中的常用函数

头文件algorithm中的常用函数 一.非修改性序列操作(12个) 循环         对序列中的每个元素执行某操作         for_each() 查找         在序列中找出某个值的第一次出现的位置         find() 在序列中找出符合某谓词的第一个元素     find_if() 在序列中找出一子序列的最后一次出现的位置         find_end() 在序列中找出第一次出现指定值集中之值的位置     find_first_of() 在序列中找出相邻的一对

C++中的算法头文件&lt;algorithm&gt;,&lt;numeric&gt;和&lt;functional&gt;

算法部分主要由头文件<algorithm>,<numeric>和<functional>组成.<algorithm>是所有STL头文件中最大的一个,它是由一大堆模版函数组成的,可以认为每个函数在很大程度上都是独立的,其中常用到的功能范围涉及到比较.交换.查找.遍历操作.复制.修改.移除.反转.排序.合并等等.相关函数:http://www.cplusplus.com/reference/algorithm/<numeric>体积很小,只包括几个在

【C++常用函数】头文件&lt;algorithm&gt;中的常用函数(绝对值,交换,比较)

swap(a,b) 用于交换a,b两个变量的值: max(a,b) 返回a,b中的最大值: min(a,b) 返回a,b中的最小值: abs(x) 返回x的绝对值,x必须是整数: 原文地址:https://www.cnblogs.com/sdtuzxr/p/11980107.html

C++学习笔记之STL标准库(二)algorithm头文件即算法

#include <algorithm> algorithm头文件中主要包含的是一大堆模板函数,即STL库提供的算法,可以认为每个函数在很大程度上是独立的.提供的算法种类有: 1)adjacent_find //检测区间内第一对相等的相邻元素 template<class FwIt> FwIt adjacent_find(FwdIt first,FwdIt last);   //如果成功,返回first+N,N满足*(first+N) == *(first+N+1):如果不存在相等

algorithm头文件的一些常用函数

首先当然要包含algorithm头文件 :#include <algorithm> max(x,y) min(x,y) 参数可以是浮点数 abs(x) x必须是整数 如果x是浮点数则要用math头文件下的fabs(x) swap(x,y) 交换x和y reverse(it,it2) it和it2可以是数组指针,也可以是容器的迭代器 注意的是翻转范围为:[it,it2) next_permutation() 给出一个序列在全排列中的下一个序列 1 int box[3]={1,2,3}; 2 do

STL_头文件

#include <string> #include <vector> #include <deque> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <algorithm> // 算法 #include <numeric> // 算法 #include &

C++ 利用&lt;cstdlib&gt; 头文件 , 产生一个random number generator

头文件<cstdlib>中有一个重要的函数 rand() , 可以作为随机数发生器. 现在现在我想产生一个随机数, 我使用如下的程序: #include <iostream> #include <ctsdlib> <span style="font-family: Arial, Helvetica, sans-serif;">using namespace std;</span> int main() {     cout &

万能头文件

原文代码 万能头文件#include<bits/stdc++.h> 最近在打cf时赛后翻阅别人的代码总是会发现一个陌生而奇怪的头文件#include<bits/stdc++.h> 奇怪之处就在于基本上所有的代码只要用了这个头文件就不再写其他头文件了. 百度过后仿佛打开了新世界的大门,头文件居然还可以这样用!!! #include<bits/stdc++.h>包含了目前c++所包含的所有头文件!!!! 从此开启开挂般的人生啊!! 现在再看下面这一堆乱七八糟的头文件显得莫名

C/C++常用头文件及函数汇总

C/C++头文件一览 C #include <assert.h> //设定插入点#include <ctype.h> //字符处理#include <errno.h> //定义错误码#include <float.h> //浮点数处理#include <iso646.h>        //对应各种运算符的宏#include <limits.h> //定义各种数据类型最值的常量#include <locale.h> //定