Algorithm feature in c++ STL
See all algorithm click here
Mutating and Non-mutating algorithms
Mutating algorithms
Mutating algorithms means this algorithm will change the content that iterator pointed to. Like copy, swap, replace, fill, remove, permutation, partition, random shuffling and sort.
If your give these algorithms a const iterator, only error will be returned.
|
|
Non-mutating algorithm
Algorithm not change any element that iterator pointed to. Like: find, search, for_each, count, equal_mismatch, max, min.
Some algorithms have functor like for_each to change element.
|
|
General form in STL algorithm
[first, last)
STL usually use open-close range which is [first, last). The range is from first to last(not include last).
The benefit is that if function stop at last position, you this function is end. For example, find() return a iterator which point to last position, that means not find the target value. You don’t need null to represent without finding.
iterator
Each algorithm need different type and is Backward compatible.
different type
That may have some different version for one function.
Some may need a functor. like, find and find_if. Usually, it appear on some function which need comparison.
For mutating algorithm, normally have two version. One is in-place and another is copy. like replace and replace_if.