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

本文介绍了C++ STL库中的常见算法,包括非质变算法如for_each、find、find_if等,质变算法如transform、swap、replace等,以及排序和查找算法如sort、binary_search、merge等,掌握这些算法能有效提升编程效率。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



