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 the Compare criteria, which defaults to stdc::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 to value.

Post:The size() of the container is reduced by the number of elements that were removed
Returns: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 predicate pred returns true.

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 removed

Returns:

Past-the-end iterator after the elements are removed

Functional

template <typename T> less

Function object for performing comparisons. Unless specialized, invokes operator< on type T.

constexpr bool operator()(T const &lhs, T const &rhs) const

Checks if lhs is less than rhs.

result_type = bool
first_argument_type = T
second_argument_type = T
template <typename T> less_equal

Function object for performing comparisons. Unless specialized, invokes operator<= on type T.

constexpr bool operator()(T const &lhs, T const &rhs) const

Checks if lhs is less or equal than rhs.

result_type = bool
first_argument_type = T
second_argument_type = T
template <typename T> greater

Function object for performing comparisons. Unless specialized, invokes operator> on type T.

constexpr bool operator()(T const &lhs, T const &rhs) const

Checks if lhs is greater than rhs.

result_type = bool
first_argument_type = T
second_argument_type = T
template <typename T> greater_equal

Function object for performing comparisons. Unless specialized, invokes operator>= on type T.

constexpr bool operator()(T const &lhs, T const &rhs) const

Checks if lhs is greater or equal than rhs.

result_type = bool
first_argument_type = T
second_argument_type = T
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, and false 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 in m_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
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, and false 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 in m_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

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’s emplace_back method.

Returns:*this
private Container *container

Index by name