C++ STL库实现了很多常用的算法,基本都在<algorithm>头文件下,掌握它们对提高开发效率很有用。主要分为三种:不会修改数据的非质变算法(Non-mutating Algorithms),会修改数据的质变算法(Mutating Algorithms),以及排序和查找算法(Sorting and Searching Algorithms)。下面就简单介绍一下主要的一些算法函数。
1.非质变算法
-
for_each
为指定序列区间应用函数fn
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);
-
find
查找等于指定值的第一个元素位置
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
-
find_if
查找满足条件的第一个元素位置
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
-
find_first_of