Standard Library AlgorithmsStandard AlgorithmsAlgorithms
Non-Modifying Operations Non-Modifying Non-Mod.
returns
true
if predicate check
yields true
for
any element in the input range; otherwise returns false
returns
true
if predicate check
yields true
for
all elements in the input range; otherwise returns false
returns
true
if predicate check
yields false
for
all elements in the input range; otherwise returns false
Find Single Elements
find the 1st matching position in range 1
of any element contained in range 2
find the 1st matching position in range 1
of any element contained in range 2
Find Consecutive Runs of Equal Elements
Find Subranges
compares 2 ranges using 3-way comparisons and returns the
result as a value of the strongest applicable comparsion category
Binary Search of Sorted Ranges Binary Search Binary Search
returns
input range must be sorted
true
if the input range contains an element
equivalent to value
; otherwise returns false;input range must be sorted
returns an iterator to the smallest element;
the 2nd version uses
the 2nd version uses
compare
for comparing elements,
while the 1st version uses operator<
returns an iterator to the largest element;
the 2nd version uses
the 2nd version uses
compare
for comparing elements,
while the 1st version uses operator<
returns a pair of iterators to both the smallest and largest elements;
the 2nd version uses
the 2nd version uses
comp
for comparing elements,
while the 1st version uses operator<
min/max/minmax/clamp
min(a, b) →
a if (a < b) is true
, b otherwise
min(a, b, f(o,o)→bool) →
a if f(a,b)
is true
, b otherwise
max(a, b) →
b if (a < b) is true
, a otherwise
max(a, b, f(o,o)→bool) →
b if f(a,b)
is true
, a otherwise
minmax(a, b) →
{ smallest , largest }
minmax(a, b, f(o,o)→bool) →
{ smallest , largest }
minmax({v1,v2,v3, …}) →
{ smallest , largest }
C++11
custom comparison function/object: f(a,b)
must return true
if a
should be ordered before b
C++17
clamps value
in the interval given by hi
and lo
;
the second version uses f
to compare values instead of operator<
return
true
if range 2 is a permutation of range 1, i.e.,
both ranges must contain the same elements, but not necessarily in the same orderCopying / Moving Elements Copying/Moving Copy/Move
copies all elements in the input range to a target (range)
beginning at
the target must be able to recieve distance(begin,end) many elements
target_begin
and returns an iterator one behind the
last target value;the target must be able to recieve distance(begin,end) many elements
copies all elements in the range starting at
the target must be able to recieve
begin
to a target (range)
beginning at target_begin
and returns an iterator one behind the
last target value;the target must be able to recieve
n
elements
copies all elements in the input range for which
the target must be able to recieve the copied elements
select
yields
true
to a target (range) beginning at tgt
and returns an iterator
one behind the last target value;the target must be able to recieve the copied elements
moves all elements in the input range to a target (range)
beginning at
the target must be able to recieve distance(begin,end) many elements
target_begin
and returns an iterator one behind the
last target value;the target must be able to recieve distance(begin,end) many elements
moves all elements in the input range to a target (range) ending
at
the target must be able to recieve distance(begin,end) many elements
target_end
and returns an iterator to the first target value;the target must be able to recieve distance(begin,end) many elements
Random Sampling
Selects
n
elements (without replacement) with equal probability from
an input population range using a random_generator (uniform random bit engine)
and writes them to an output range.Reordering Elements Reordering Reorder
return
true
if range 2 is a permutation of range 1, i.e.,
both ranges must contain the same elements, but not necessarily in the same orderNumeric Operations Numeric Operations Numeric
#include <numeric>
std:: Algorithms are
- algorithmic building blocks
- operating on (iterator) ranges of elements
- implemented as free-standing functions
- generic: implemented in a (mostly) container/element-agnostic way
- many are customizable with function(object)s / lambdas
- well-tested and efficient