STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorithm>,对于数值算法须包含<numeric>,<functional>中则定义了一些模板类,用来声明函数对象.
STL中算法大致分为四类:
1、非可变序列算法:指不直接修改其所操作的容器内容的算法.
2、可变序列算法:指可以修改它们所操作的容器内容的算法.
3、排序算法:包括对序列进行排序和合并的算法、搜索算法以及有序序列上的集合操作.
4、数值算法:对容器内容进行数值计算.
<一>查找算法(13个):判断容器中是否包含某个值
adjacent_find :在 iterator 对标志的元素范围内,查找一对相邻的重复元素,如果找到返回一个 ForwardIterator,指向这对元素的第一个元素.否则返回 last .重载版本使用输入的二元操作符代替相等的判断.
参数:first
:last
返回:forwardItr
template<class forwardItr>
forwardItr adjacent_find(forwardItr first, forwardItr last);
参数:first
:last
:op
返回:forwardItr
template<class forwardItr, class binaryPredicate>
forwardItr adjacent_find(forwardItr first, forwardItr last, binaryPredicate op);
#include "stdafx.h" #include <string> #include <iostream> #include <iterator> #include <vector> #include <algorithm> #include <numeric> #include <functional> //x,y为依次相邻的两个元素 bool isequal(int x, int y) { return x / 2 == y; } int main() { { std::vector<int> vec = { 12, 22, 11, 33, 33, 43 }; auto pos1 = adjacent_find(vec.begin(), vec.end()); std::cout << *pos1 << std::endl; //输出:33 auto pos2 = adjacent_find(vec.begin(), vec.end(), isequal); std::cout << *pos2 << std::endl; //输出:22 getchar(); return 0; } }