STL Complement¶
A few small goodies I wish were part of the Standard Template Library. It is not intended to contain complex algorithms that would be candidates for Boost or other libraries, just small variations, adaptations and idioms constructed over what’s already available.
The namespace used for all utilities here is stdc
, which keeps it short and
reminds of the complement nature to the std
namespace.
Contents:
Algorithm¶
Functions that, in the same spirit of the STL, complement the <algorithm>
header.
-
template <typename T, typename Compare = stdc::less<T>, typename... Rest> constexpr bool is_sorted(T first, T second, Rest... rest)
Checks if the elements
[first,second,rest...]
are sorted in non-descending order according to theCompare
criteria, which defaults tostdc::less<T>
-
template <typename Container> typename Container::iterator erase_remove(Container& container, typename Container::value_type const& value)
Removes all elements from
container
that are equal tovalue
.Post: The size()
of the container is reduced by the number of elements that were removedReturns: Past-the-end iterator after the elements are removed
-
template <typename Container, typename UnaryPredicate> typename Container::iterator erase_remove_if(Container& container, UnaryPredicate pred)
Removes all elements from
container
for which predicatepred
returnstrue
.Parameters: - container – The container from which elements will be removed
- pred – The predicate to be applied to all elements in the container
Post: The
size()
of the container is reduced by the number of elements that were removedReturns: Past-the-end iterator after the elements are removed
Dynamic Array¶
-
template <typename T> dynamic_array
Container that dynamically allocates memory so that the elements are permanently stored in contiguous memory. Elements are written and accessed using the
operator[]
or a pointer to the first element withdata()
.-
Container = std::vector<T>
-
value_type = T
-
reference = value_type&
-
const_reference = value_type const&
-
size_type = typename Container::size_type
-
reference
operator[]
(size_type index)¶ Parameters: index – Index of the element to be retrieved. Returns: The index
-th element of the array. If that index has not beenfilled yet, a default-inserted element is returned.
-
const_reference
operator[]
(size_type index) const¶ Parameters: index – Index of the element to be retrieved. Returns: The index
-th element of the array. If that index has not beenfilled yet, an
std::out_of_range
exception is thrown.
-
size_type
size
() const¶ Returns: The number of elements currently held by the array.
-
T *
data
()¶ Returns: A pointer to the first element of the array.
-
T const *
data
() const¶ Returns: A pointer to the first element of the array.
-
Functional¶
-
template <typename T> less
Function object for performing comparisons. Unless specialized, invokes
operator<
on typeT
.-
constexpr bool
operator()
(T const &lhs, T const &rhs) const¶ Checks if
lhs
is less thanrhs
.
-
result_type = bool
-
first_argument_type = T
-
second_argument_type = T
-
constexpr bool
-
template <typename T> less_equal
Function object for performing comparisons. Unless specialized, invokes
operator<=
on typeT
.-
constexpr bool
operator()
(T const &lhs, T const &rhs) const¶ Checks if
lhs
is less or equal thanrhs
.
-
result_type = bool
-
first_argument_type = T
-
second_argument_type = T
-
constexpr bool
-
template <typename T> greater
Function object for performing comparisons. Unless specialized, invokes
operator>
on typeT
.-
constexpr bool
operator()
(T const &lhs, T const &rhs) const¶ Checks if
lhs
is greater thanrhs
.
-
result_type = bool
-
first_argument_type = T
-
second_argument_type = T
-
constexpr bool
-
template <typename T> greater_equal
Function object for performing comparisons. Unless specialized, invokes
operator>=
on typeT
.-
constexpr bool
operator()
(T const &lhs, T const &rhs) const¶ Checks if
lhs
is greater or equal thanrhs
.
-
result_type = bool
-
first_argument_type = T
-
second_argument_type = T
-
constexpr bool
-
template <typename T, typename Compare = std::less<T>> less_than
Unary predicate that is constructed with a given pivot element and then returns
true
for elements that are less than the pivot, andfalse
otherwise.-
explicit
less_than
(T const &pivot, Compare compare = Compare())¶ Constructor. Saves a copy of the pivot in
m_pivot
and a copy of the compare object inm_compare
.
-
bool
operator()
(T const &value) const¶ Returns the value of evaluating
m_compare(value, m_pivot)
.
-
result_type = bool
-
argument_type = T
-
protected T
m_pivot
¶
-
protected Compare
m_compare
¶
-
explicit
-
template <typename T, typename Compare = std::greater<T>> greater_than
Unary predicate that is constructed with a given pivot element and then returns
true
for elements that are greater than the pivot, andfalse
otherwise.-
explicit
greater_than
(T const &pivot, Compare compare = Compare())¶ Constructor. Saves a copy of the pivot in
m_pivot
and a copy of the compare object inm_compare
.
-
bool
operator()
(T const &value) const¶ Returns the value of evaluating
m_compare(value, m_pivot)
.
-
result_type = bool
-
argument_type = T
-
protected T
m_pivot
¶
-
protected Compare
m_compare
¶
-
explicit
-
template <typename T> equal_to
Unary predicate that is constructed with a given pivot element and then returns
true
for elements that compare equal to the pivot.-
explicit equal_to
Constructor. Saves a copy of the pivot in
m_pivot
.
-
template <typename U> bool operator()(U const& value) const
Returns the value of evaluating
std::equal_to<T>(value, m_pivot)
.value
is implicitly converted to typeT
.
-
result_type = bool
-
argument_type = T
-
protected T
m_pivot
¶
-
Iterator¶
-
template <typename Container> back_emplacer_operator
Function object that calls
emplace_back()
on the container for which it was constructed,std::forward
‘ing the arguments to the container.-
explicit
back_emplacer_operator
(Container &container)¶ Parameters: container – Reference to the container that supports emplace_back
Type container: Container&
-
template <typename... Args> back_emplacer_operator<Container>& operator()(Args&&...)
Calls
std::forward
on the given arguments to pass them to the container’semplace_back
method.Returns: *this
-
private Container *
container
¶
-
explicit