C++ 20

Note: I will not be including projections in this or any of these articles

Info

std::all_of checks to see if a given unary predicate returns true for all values in a range

Complexity: Linear, O(n)

Category: Non-Modifying Sequence Operations

Breakdown

std::all_of:

Parameters

  • first = Input Iterator to the start of the range
  • last = Input Iterator to the end of the range
  • pred = A unary predicate where pred(x) is a boolean value. Where x is a value in the range [first,last)

Returns

  • true if pred(x) returns true for all values in range [first,last)
  • false if pred(x) returns false for one or more value in range [first,last)

std::ranges::all_of:

Parameters

  • r = The range to operate on (i.e. std::vector, std::set, ect.)
  • pred = A unary predicate where pred(x) is a boolean value. Where x is a value in range r

Returns

  • true if pred(x) returns true for all values in range r
  • false if pred(x) returns false for one or more value in range r

Use Cases

  • Checking if all values in a range are positive or negative
  • Checking if all values in a range are greater than or less than a value
  • Checking if all values in a range are even or odd
  • Checking if all strings in a range are not empty or equal a certain length

Examples

Keywords

  • Input Iterator: Iterator used to read values from a container. Can only be used in sequential input operations. Each value it points to is read once then the iterator is incremented.
  • Lambda Function: Convenient way of defining an annonymous function right where it is used or passed as an argument. A simplified format, [lambda introducer](parameter)->return type{body;}
  • Non-Modifying Sequence Operation: An algorithm that does not modify the values in the container. It just reads every value in the container.
  • Predicate: Function that returns a boolean.
  • Range: A group of items you can iterate over.
  • Unary: A single input.

Sources

Im just here to learn some things