注意:这篇文章上次更新于1537天前,文章内容可能已经过时。
This article was last updated1537 days ago, the content may be outdated.
Algorithms That Modify Sequences
copy
Copies elements in a container range to another location.
Input parameters:
- Begin iterator
- End iterator
- Begin iterator of the copy destination
Function behavior:
1 | template <class InputIterator, class OutputIterator> |
示例代码:
Example code:
1 | // copy algorithm example |
copy_n
Copies n elements from a container to another location
Parameters:
- Begin iterator
- Number of elements to copy, n
- Begin iterator of the copy destination
Function behavior:
1 | template <class InputIterator, class Size, class OutputIterator> |
示例代码:
Example code:
1 | // copy_n algorithm example |
copy_if
Copies elements in a range that satisfy the condition to the destination
Parameters:
- Begin iterator
- End iterator
- Begin iterator of the copy destination
- A unary function returning bool
Function behavior:
1 | template <class InputIterator, class OutputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 | // copy_if example |
copy_backward
Reverse copy (see the function behavior for details)
Parameters:
- Begin iterator
- End iterator
- Begin iterator of the copy destination
Function behavior:
1 | template <class BidirectionalIterator1, class BidirectionalIterator2> |
示例代码:
Example code:
1 | // copy_backward example |
move
Moves elements; the behavior is similar to copy, except it uses move semantics.
Parameters:
- Begin iterator
- End iterator
- Begin iterator of the move destination
Function behavior:
1 | template <class InputIterator, class OutputIterator> |
示例代码:
Example code:
1 | // move algorithm example |
1 | template <class BidirectionalIterator1, class BidirectionalIterator2> |
示例代码:
Example code:
1 | // move_backward example |
swap
Swaps two elements, or two containers with the same number of elements.
The C++ 11 version uses move semantics.
Function behavior:
1 | template <class T> |
示例代码:
Example code:
1 | // swap algorithm example (C++98) |
swap_ranges
Range swap
Parameters:
- Begin iterator
- End iterator
- Begin iterator of the second container
Function behavior:
1 | template <class ForwardIterator1, class ForwardIterator2> |
示例代码:
Example code:
1 | // swap_ranges example |
1 | template <class ForwardIterator1, class ForwardIterator2> |
示例代码:
Example code:
1 | // iter_swap example |
transform 🚩
改变序列元素,根据某个规则。这应该是比较常用的算法了。
参数:
- 起始迭代器
- 终止迭代器
- 结果起始迭代器
- 一元函数或二元函数(如果是二元函数 还需要传入 first2)
函数行为:
transform 🚩
Changes sequence elements according to some rule. This should be one of the more commonly used algorithms.
Parameters:
- Begin iterator
- End iterator
- Begin iterator of the result
- A unary or binary function (if it’s a binary function, you also need to pass first2)
Function behavior:
1 | template <class InputIterator, class OutputIterator, class UnaryOperator> |
示例代码:
Example code:
1 | // transform algorithm example |
1 | template <class ForwardIterator, class T> |
示例代码:
Example code:
1 | // replace algorithm example |
replace_if
Conditional replace
Parameters:
- Begin iterator
- End iterator
- Unary condition function
- New value
Function behavior:
1 | template <class ForwardIterator, class UnaryPredicate, class T> |
示例代码:
Example code:
1 | // replace_if example |
replace_copy
Copy and replace (basically copy and replace merged together 😂)
Parameters:
- Begin iterator
- End iterator
- Copy destination iterator
- Old value
- New value
Function behavior:
1 | template <class InputIterator, class OutputIterator, class T> |
示例代码:
Example code:
1 | // replace_copy example |
replace_copy_if
The if version of the previous function
Parameters:
- Begin iterator
- End iterator
- Begin iterator of the copy destination
- Unary condition function
- New value
Function behavior:
1 | template <class InputIterator, class OutputIterator, class UnaryPredicate, class T> |
示例代码:
Example code:
1 | // replace_copy_if example |
fill
Sets elements in the range to a specified value
Parameters:
- Begin iterator
- End iterator
- Value
Function behavior:
1 | template <class ForwardIterator, class T> |
示例代码:
Example code:
1 | // fill algorithm example |
fill_n
The specified-count version of the previous function.
Parameters:
- Begin iterator
- Count
- Value
Function behavior:
1 | template <class OutputIterator, class Size, class T> |
示例代码:
Example code:
1 | // fill_n example |
generate
fill fills a specified value, while the values filled by this function can be obtained from a custom function.
Parameters:
- Begin iterator
- End iterator
- A no-argument function whose return value type is the element value type
Function behavior:
1 | template <class ForwardIterator, class Generator> |
示例代码:
Example code:
1 | // generate algorithm example |
generate_n
The specified-count version of the previous function.
Parameters:
- Begin iterator
- Count
- A no-argument function whose return value type is the element value type
Function behavior:
1 | template <class OutputIterator, class Size, class Generator> |
示例代码:
Example code:
1 | // generate_n example |
remove🚩
Removes elements with a specified value from the container (looking at this source code, it doesn’t seem very easy to use)
Parameters:
- Begin iterator
- End iterator
- Value
Function behavior:
1 | template <class ForwardIterator, class T> |
示例代码:
Example code:
1 | // remove algorithm example |
remove_if
The if version of the previous function
Parameters:
- Begin iterator
- End iterator
- Predicate function
Function behavior:
1 | template <class ForwardIterator, class UnaryPredicate> |
示例代码:
Example code:
1 | // remove_if example |
remove_copy
Remove and copy
Parameters:
- Begin iterator
- End iterator
- Copy destination iterator
- Value
Function behavior:
1 | template <class InputIterator, class OutputIterator, class T> |
示例代码:
Example code:
1 | // remove_copy example |
remove_copy_if
The if version of the previous function
- Begin iterator
- End iterator
- Copy destination iterator
- Predicate function
Function behavior:
1 | template <class InputIterator, class OutputIterator, class UnaryPredicate> |
示例代码:
Example code:
1 | // remove_copy_if example |
unique
对容器中连续相等的元素进行去重。其中第三个参数可选,相当于 unique 和 unique_if 合在一起了,不明白为什么不加一个 unique_if 函数。
- 起始迭代器
- 终止迭代器
- 用于相邻元素的比较函数(可选)
函数行为:
unique
Removes consecutive equal elements in the container. The third parameter is optional, which is equivalent to merging unique and unique_if together — I don’t understand why they didn’t just add a unique_if function.
- Begin iterator
- End iterator
- Comparison function for adjacent elements (optional)
Function behavior:
1 | template <class ForwardIterator> |
示例代码:
Example code:
1 | // unique algorithm example |
1 | template <class InputIterator, class OutputIterator> |
示例代码:
Example code:
1 | // unique_copy example |
reverse🚩
Reverse — this one is also used a lot.
Parameters:
- Begin iterator
- End iterator
Function behavior:
1 | template <class BidirectionalIterator> |
示例代码:
Example code:
1 | // reverse algorithm example |
reverse_copy
The copy version of the previous function.
Parameters:
- Begin iterator
- End iterator
- Copy destination iterator
Function behavior:
1 | template <class BidirectionalIterator, class OutputIterator> |
示例代码:
Example code:
1 | // reverse_copy example |
1 | template <class ForwardIterator> |
示例代码:
Example code:
1 | // rotate algorithm example |
rotate_copy
The copy version of the previous function
It directly calls copy twice — quite elegant.
Function behavior:
1 | template <class ForwardIterator, class OutputIterator> |
示例代码:
Example code:
1 | // rotate_copy algorithm example |
random_shuffle
Randomly shuffles the sequence.
There are two versions — one with a specified random function, one without.
See the example code for details
Function behavior:
1 | template <class RandomAccessIterator, class RandomNumberGenerator> |
示例代码:
Example code:
1 | // random_shuffle example |
1 | template <class RandomAccessIterator, class URNG> |
示例代码:
Example code:
1 | // shuffle algorithm example |


