std::ranges::find_last, std::ranges::find_last_if, std::ranges::find_last_if_not

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
Operations on uninitialized storage
(C++17)
(C++17)
(C++17)
C library
 
Constrained algorithms
Non-modifying sequence operations
ranges::find_lastranges::find_last_ifranges::find_last_if_not
(C++23)(C++23)(C++23)

Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutations
Constrained numeric operations
Fold operations
Operations on uninitialized storage
Return types
 
Defined in header <algorithm>
Call signature
template< std::forward_iterator I, std::sentinel_for<I> S,

          class T, class Proj = std::identity >
requires std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>,
                                        const T*>
constexpr ranges::subrange<I>

  find_last( I first, S last, const T& value, Proj proj = {} );
(1) (since C++23)
template< ranges::forward_range R, class T, class Proj = std::identity >

requires std::indirect_binary_predicate<ranges::equal_to,
                                        std::projected<ranges::iterator_t<R>, Proj>,
                                        const T*>
constexpr ranges::borrowed_subrange_t<R>

  find_last( R&& r, const T& value, Proj proj = {} );
(2) (since C++23)
template< std::forward_iterator I, std::sentinel_for<I> S,

          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr ranges::subrange<I>

  find_last_if( I first, S last, Pred pred, Proj proj = {} );
(3) (since C++23)
template< ranges::forward_range R, class Proj = std::identity,

          std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
              Pred >
constexpr ranges::borrowed_subrange_t<R>

  find_last_if( R&& r, Pred pred, Proj proj = {} );
(4) (since C++23)
template< std::forward_iterator I, std::sentinel_for<I> S,

          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr ranges::subrange<I>

  find_last_if_not( I first, S last, Pred pred, Proj proj = {} );
(5) (since C++23)
template< ranges::forward_range R, class Proj = std::identity,

          std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
              Pred >
constexpr ranges::borrowed_subrange_t<R>

  find_last_if_not( R&& r, Pred pred, Proj proj = {} );
(6) (since C++23)

Returns the last element in the range [first, last) that satisfies specific criteria:

1) find_last searches for an element equal to value
3) find_last_if searches for the last element in the range [first, last) for which predicate pred returns true
5) find_last_if_not searches for the last element in the range [first, last) for which predicate pred returns false
2,4,6) Same as (1,3,5), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Parameters

first, last - the range of elements to examine
r - the range of the elements to examine
value - value to compare the elements to
pred - predicate to apply to the projected elements
proj - projection to apply to the elements

Return value

1,2,3) Let i be the last iterator in the range [first,last) for which E is true. Returns ranges::subrange<I>{i, last}, or ranges::subrange<I>{last, last} if no such iterator is found.
2,4,6) Same as (1,2,3) but the return type is ranges::borrowed_subrange_t<I>.

Complexity

At most last - first applications of the predicate and projection.

Example

See also

finds the last sequence of elements in a certain range
(niebloid)
finds the first element satisfying specific criteria
(niebloid)
searches for a range of elements
(niebloid)
determines if an element exists in a partially-ordered range
(niebloid)
checks if the range contains the given element or subrange
(niebloid)