注意:这篇文章上次更新于1579天前,文章内容可能已经过时。
This article was last updated1579 days ago, the content may be outdated.
提到 STL 算法,大家都知道很牛B。
但是想一想,除了 sort() , 你还会用啥呢?😂
最近抽时间把 STL 所有算法测试一遍,做到心里有个印象。
本文的这些示例基本上都来自于 cppreference.com 和 cplusplus.com
其实就是挨个看一遍,然后复制粘贴一下。
STL algorithms — everyone knows they are awesome.
But think about it: apart from sort(), what else do you actually use? 😂
I recently took some time to test all STL algorithms one by one, so I have a rough idea of what each one does.
The examples in this article basically come from cppreference.com and cplusplus.com
In fact, I just went through them one by one and copy-pasted.
Algorithms That Do Not Modify Sequences
all_of
Checks whether the predicate holds for all elements; returns true if so, false otherwise.
Input parameters:
- Begin iterator
- End iterator
- Callable object (predicate condition)
Function behavior:
1 | template <class InputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 |
|
any_of
Checks whether the predicate holds for some elements; returns true if so, false otherwise.
Input parameters:
- Begin iterator
- End iterator
- Callable object (predicate condition)
Function behavior:
1 | template <class InputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 |
|
none_of
Checks whether the predicate holds for no elements; returns true if so, false otherwise.
Input parameters:
- Begin iterator
- End iterator
- Callable object (predicate condition)
Function behavior:
1 | template <class InputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 |
|
for_each
Input parameters:
- Begin iterator
- End iterator
- Callable object fn
Applies fn to every element.
Function behavior:
1 | template <class InputIterator, class Function> |
示例代码:
Example code:
1 |
|
find
Input parameters:
- Begin iterator
- End iterator
- The object to look for
Returns the iterator of the matching element.
PS: As you can see from the function behavior, custom objects need to overload the == operator.
Function behavior:
1 | template <class InputIterator, class T> |
示例代码:
Example code:
1 | // find example |
find_if
Input parameters:
- Begin iterator
- End iterator
- Callable object
When the callable object returns true, returns the iterator of that element; if not found, returns the end iterator.
Function behavior:
1 | template <class InputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 | // find_if example |
find_if_not
Basically the same as above.
Returns the iterator of the first element that does NOT satisfy the condition; if not found, returns the end iterator.
Function behavior:
1 | template <class InputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 | // find_if_not example |
find_end
输入参数:
- 序列①起始迭代器
- 序列①终止迭代器
- 序列②起始迭代器
- 序列②终止迭代器
- 比较条件(重载版本,默认条件是 == )
在序列①中查找序列②最后一次出现的位置,查找成功返回序列①中相应的迭代器,查找失败返回序列①的终止迭代器。
函数行为:
find_end
Input parameters:
- Begin iterator of sequence ①
- End iterator of sequence ①
- Begin iterator of sequence ②
- End iterator of sequence ②
- Comparison predicate (overloaded version; the default is ==)
Finds the last occurrence of sequence ② in sequence ①; returns the corresponding iterator in sequence ① on success, or the end iterator of sequence ① on failure.
Function behavior:
1 | template <class ForwardIterator1, class ForwardIterator2> |
示例代码:
Example code:
1 | // find_end example |
find_first_of
输入参数:
- 序列①起始迭代器
- 序列①终止迭代器
- 序列②起始迭代器
- 序列②终止迭代器
- 比较条件(重载版本,默认条件是 == )
在序列①中进行查找,返回首先满足该元素存在于序列②中的元素迭代器,查找失败返回第一个序列的终止迭代器。
PS: 这个函数和上一个不太一样,上一个是查全部,这个不是。
函数行为:
find_first_of
Input parameters:
- Begin iterator of sequence ①
- End iterator of sequence ①
- Begin iterator of sequence ②
- End iterator of sequence ②
- Comparison predicate (overloaded version; the default is ==)
Searches sequence ① and returns the iterator of the first element that also exists in sequence ②; if not found, returns the end iterator of the first sequence.
PS: This function is different from the previous one — the previous one searches for the whole sub-sequence, this one doesn’t.
Function behavior:
1 | template <class InputIterator, class ForwardIterator> |
示例代码:
Example code:
1 | // find_first_of example |
adjacent_find
Input parameters:
- Begin iterator
- End iterator
- Comparison predicate (overloaded version; the default is ==)
Finds the first pair of equal adjacent elements in the sequence; if not found, returns the end iterator.
Function behavior:
1 | template <class ForwardIterator> |
示例代码:
Example code:
1 | // adjacent_find example |
count
输入参数:
- 起始迭代器
- 终止迭代器
- 比较对象
在序列中统计与目标对象相等的元素数量。
PS:没有传入比较条件的参数(传入比较条件的算法叫 count_if ),意味着可能需要重载 == 运算符。
函数行为:
count
Input parameters:
- Begin iterator
- End iterator
- The object to compare with
Counts the number of elements in the sequence that are equal to the target object.
PS: There is no comparison-predicate parameter (the algorithm that takes one is called count_if), which means you may need to overload the == operator.
Function behavior:
1 | template <class InputIterator, class T> |
示例代码:
Example code:
1 | // count algorithm example |
1 | template <class InputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 | // count_if example |
mismatch🚩
输入参数:
- 序列①起始迭代器
- 序列①终止迭代器
- 序列②起始迭代器
- 条件函数(重载版本)
一次比较序列①和序列②,返回第一次匹配失败的位置。返回为 pair 类型,分别记录两个序列第一个不匹配的迭代器。
函数行为:
mismatch🚩
Input parameters:
- Begin iterator of sequence ①
- End iterator of sequence ①
- Begin iterator of sequence ②
- Comparison function (overloaded version)
Compares sequence ① and sequence ② element by element, and returns the position of the first mismatch. The return value is a pair type, recording the first mismatching iterators of both sequences.
Function behavior:
1 | template <class InputIterator1, class InputIterator2> |
示例代码:
Example code:
1 | // mismatch algorithm example |
都不需要检查第二个迭代器是否到头吗?
1 2 3 4 5 6
1 2 3
这样传两个序列岂不是 *first2 就越界了?
测试了以下,果然会乱七八糟。
Isn’t it necessary to check whether the second iterator has reached its end?
1 2 3 4 5 6
1 2 3
If you pass two sequences like this, wouldn’t *first2 go out of bounds?
I tested it, and sure enough it went all over the place.

equal
Input parameters:
- Begin iterator of sequence ①
- End iterator of sequence ①
- Begin iterator of sequence ②
- Comparison function (overloaded version)
Checks whether the sequence starting at first2 in sequence ② contains sequence ①.
Function behavior:
1 | template <class InputIterator1, class InputIterator2> |
示例代码:
Example code:
1 | // equal algorithm example |
is_permutation🚩
Input parameters:
- Begin iterator of sequence ①
- End iterator of sequence ①
- Begin iterator of sequence ②
- Comparison function (overloaded version)
Checks whether sequence ① is a permutation of sequence ②.
Function behavior:
1 | template <class InputIterator1, class InputIterator2> |
示例代码:
Example code:
1 | // is_permutation example |
search
输入参数:
- 序列①起始迭代器
- 序列①终止迭代器
- 序列②起始迭代器
- 序列②终止迭代器
- 比较条件(重载版本,默认条件是 == )
返回序列②在序列①中第一次出现的位置。
PS:与 find_end 对应。
函数行为:
search
Input parameters:
- Begin iterator of sequence ①
- End iterator of sequence ①
- Begin iterator of sequence ②
- End iterator of sequence ②
- Comparison predicate (overloaded version; the default is ==)
Returns the position where sequence ② first appears in sequence ①.
PS: It’s the counterpart of find_end.
Function behavior:
1 | template <class ForwardIterator1, class ForwardIterator2> |
示例代码:
Example code:
1 | // search algorithm example |
search_n
Input parameters:
- Begin iterator
- End iterator
- Minimum number of consecutive matching elements
- The object to compare with
- Comparison function (overloaded version)
Finds the starting position of n consecutive elements equal to the target object in the sequence.
Function behavior:
1 | template <class ForwardIterator, class Size, class T> |
示例代码:
Example code:
1 | // search_n example |
BibiBibi
今天就先看到这了,该睡了。
留个思考题,为什么 mismatch , is_permutation , equal 都不需要传第二个容器的终止迭代器呢?而 search 和 find_end 却需要传入第二个容器的终止迭代器。
后半部分应该好回答,因为函数要实现的功能要求它必须要知道两个序列的长度。
对于前半部分还需要再研究研究,今天太晚了,脑子不清醒了。
难道是因为 C++ 设计理念的原因?我听过一种说法,C++ 是一门充分相信程序员的语言,Java 是一门充分不相信程序员的语言。也许这就是原因吧😂
02点30分
BibiBibi
That’s all I looked at today — time to sleep.
A thought question: why don’t mismatch, is_permutation, and equal need the end iterator of the second container, while search and find_end do?
The latter part should be easy to answer: the functionality those functions implement requires them to know the length of both sequences.
The former part still needs more research — it’s too late today and my mind is foggy.
Could it be due to C++'s design philosophy? I’ve heard a saying: C++ is a language that fully trusts programmers, while Java is a language that fully distrusts programmers. Maybe that’s the reason 😂
2:30 AM


