Backports related to Standard header <algorithm>
Interfaces
Backports defined in this section:
all_of()
, any_of()
, none_of()
(C++11) - check if elements of a sequence fit a predicate.
copy_if()
, copy_n()
, partition_copy()
(C++11) - copy elements from a sequence.
find_if_not()
(C++11) - check if elements don't fit a predicate.
is_permutation()
, next_permutation()
, prev_permutation()
(C++11) - check the sorting status of a sequence.
is_sorted()
(C++11) - check the sorting status of a sequence.
minmax()
, minmax_element()
(C++11) - minimum and maximum elements in a sequence.
equal()
, mismatch()
overloads (C++14) - compare sequences.
clamp()
(C++17) - constrain values.
count()
, count_if()
(C++17) - count elements in a sequence.
for_each_n()
(C++17) - execute functors across a sequence.
shift_left()
, shift_right()
(C++20) - slide a sequence.
Pending:
Non-backport features:
cxxomfort::fix::static_clamp
- constrain numeric values as template arguments.
algorithms
This header offers a number of constexpr
-capable implementations for some algorithms that can be made constexpr
with only C++11 requirement. Because the names can not be reused when the algorithms are already provided by C++ (since they'd take the same argumentts in the same order), they are instead provided in two ways:
- as overloads with more arguments eg:
cxxomfort::fix::count( cxxo_constexpr, ini, fin, v)
.
- as functions with a different name eg:
cxxomfort::fix::static_count( ini, fin, v)
.
These algorithms are:
cxxomfort::fix::constexpr_clamp
cxxomfort::fix::constexpr_count
cxxomfort::fix::constexpr_count_if
cxxomfort::fix::constexpr_find
cxxomfort::fix::constexpr_find_if
cxxomfort::fix::constexpr_equal
- Note
- These algorithms can only work constexpr with native arrays, and with
constexpr
types whose iterators are pointer types (eg.: basic_string
with default allocator).
Demonstration:
Copies and prints elements from a list via copy_if()
and for_each_n()
.
struct {
template <typename Numeric>
bool operator() (Numeric n) const {
return n % 2 == 0;
}
} even= {};
struct {
template <typename T>
SS& operator() (T const& t) const {
return os<< t;
}
std::ostream& os;
} print (std::cout);
list<int> s1;
generate( begin(s1), end(s1), rand);
vector<int> s2;
copy_if( begin(s1), end(s1), back_inserter(s2), even);
Examples
For other examples see the following entries in cxxomfort-examples
:
randomly - usage of various algorithms.
- More pending...
See Also
- See also
- cxxomfort/algorithm.hpp (header reference)
-
header/algorithm @ cppreference (cppreference)
-
<numeric> (std numeric algorithms) , Cxxomfort <algorithm> Supplements (supplemental)
- Member cxxomfort::cxxostd::copy_if (InpI ini, InpI fin, OutI dest, Predicate f)
- Copy elements from one place to another
- Member cxxomfort::cxxostd::copy_n (InpI ini, Integral n, OutI dest)
- "Algorithms" "copy_n"
- Member cxxomfort::cxxostd::count (It ini, It fin, T const &tv)
- Member cxxomfort::cxxostd::count_if (It ini, It fin, Pred p)
- Member cxxomfort::cxxostd::equal (RangeIt1 ini1, RangeIt1 fin1, RangeIt2 ini2, RangeIt2 fin2, Compare eq)
- ""
- Member cxxomfort::cxxostd::fill_n (It ini, Numeric n, T const &tv)
- Member cxxomfort::cxxostd::find_if (It ini, It fin, Pred f)
- Member cxxomfort::cxxostd::find_if_not (It ini, It fin, Pred f)
- "Algorithms" find_if_not
- Member cxxomfort::cxxostd::for_each_n (II first, Size n, UnaryFunction f)
-
- Member cxxomfort::cxxostd::is_sorted (Iterator ini, Iterator fin, Compare less)
- "Algorithms" is_sorted
- Member cxxomfort::cxxostd::minmax (T const &a, T const &b, Comparator Less)
- Find the minimum and maximum elements in a sequence
- Member cxxomfort::cxxostd::minmax_element (FIterator ini, FIterator fin, Comparator less)
- Member cxxomfort::cxxostd::mismatch (InputIt1 ini1, InputIt1 fin1, InputIt2 ini2, InputIt2 fin2, BinaryPredicate p)
- ""
- Member cxxomfort::cxxostd::next_permutation (BidirIt ini, BidirIt fin, Compare Less)
- Member cxxomfort::cxxostd::partition_copy (InpI ini, InpI const fin, OutI1 Dtrue, OutI2 Dfalse, Predicate f)
- Conditionally copy data to either of two destinations. partition_copy
- Member cxxomfort::cxxostd::prev_permutation (BidirIt ini, BidirIt fin, Compare less)
- Member cxxomfort::cxxostd::shuffle (RAIterator ini, RAIterator fin, URNG &g)
"Algorithms" shuffle
- Member cxxomfort::move (IIt ini, IIt fin, OIt dest)
- Member cxxomfort::move_backward (IIt ini, IIt fin, OIt dest)