Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | algorithm‎ | ranges
Revision as of 05:09, 29 July 2022 by Space Mission (Talk | contribs)

 
 
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
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
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.

Contents

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)[edit]
finds the first element satisfying specific criteria
(niebloid)[edit]
searches for the first occurrence of a range of elements
(niebloid)[edit]
determines if an element exists in a partially-ordered range
(niebloid)[edit]
checks if the range contains the given element or subrange
(niebloid)[edit]