algorithm
Overview
The estd::algorithm
provides a set of well-optimized and tested functions
that can be used to perform a variety of operations on ranges of elements. It
can be used with containers such as estd::array
, estd::vector
,
estd::deque
, etc., across ::estd
.
Some of the functions provided by estd::algorithm
include min()
,
max()
, swap()
, equals()
, not_equals()
, greater1()
,
less1()
, all_of()
, any_of(),
and none_of()
. The algorithm uses
iterators to iterate through these range of elements in the container. The
functions min()
, max()
, and swap()
differ from other algorithms
because they do not use any iterators or containers, but instead operate
directly using values passed to them.
estd::UnaryPredicate
is used in estd::algorithm
to check conditions and
perform operations based on those conditions. It takes one argument and returns
a boolean value.
The estd::algorithm
contains algorithm, which is not part of C++98
, is
written for compatibility with C++03
. The estd algorithm uses iterators
for the access to containers. Kindly refer the respective container for the
element access and iterators.
Usage
The usage constraints and guidelines apply for algorithm:
Constraints
The containers used in algorithm should support iterators.
Usage guidance
Valid arguments/iterators should be passed to the function. Usage of empty containers should be avoided.
Example
Here are the examples to create container using different estd containers.
// example to create container c from array
::estd::array<int32_t, 3> c = {{1, 2, 3}};
// example to create container c from vector
::estd::declare::vector<int32_t, 10U> c0;
// example to create container c1 from deque
::estd::declare::deque<int32_t, 10U> c1;
// Checks if all of the value in array c are equal to 1.
::estd::all_of(c.cbegin(), c.cend(), ::estd::equals<int32_t>(1));
// Checks if all of the value in vector c0 are equal to 1.
::estd::all_of(c0.cbegin(), c0.cend(), ::estd::equals<int32_t>(1));
// Checks if all of the value in deque c1 are equal to 1.
::estd::all_of(c1.cbegin(), c1.cend(), ::estd::equals<int32_t>(1));
c1.push_back(1); // appending value to the container c1
// Returns the minimum value of two integers which is 10U.
::estd::min(15U, 10U);
// Returns the maximum value of two integers which is 15U.
::estd::max(15U, 10U);
// Swaps the values of two variables.
uint16_t a = 1, b = 2;
::estd::swap(a, b);
// Check all of the elements in container equals to 1.
::estd::all_of(c.cbegin(), c.cend(), ::estd::equals<int32_t>(1));
// Check if all of the elements in container not equals to 1.
::estd::all_of(c.cbegin(), c.cend(), ::estd::not_equals<int32_t>(1));
// Check if all of the elements in container are greater than a certain value(0).
::estd::all_of(c.cbegin(), c.cend(), ::estd::greater1<int32_t>(0));
// Check if all of the elements in container are less than a certain value(0).
::estd::all_of(c.cbegin(), c.cend(), ::estd::less1<int32_t>(10));
::estd::any_of(c.cbegin(), c.cend(), ::estd::equals<int32_t>(1));
::estd::none_of(c.cbegin(), c.cend(), ::estd::equals<int32_t>(-1));