Namespaces
Variants

std::ranges::adjacent_find

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
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
(C++11)    

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


 
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 Proj = std::identity,
          std::indirect_binary_predicate
              <std::projected<I, Proj>,
               std::projected<I, Proj>> Pred = ranges::equal_to >
constexpr I
    adjacent_find( I first, S last, Pred pred = {}, Proj proj = {} );
(1) (since C++20)
template< ranges::forward_range R, class Proj = std::identity,
          std::indirect_binary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>,
               std::projected<ranges::iterator_t<R>, Proj>> Pred
              = ranges::equal_to >
constexpr ranges::borrowed_iterator_t<R>
    adjacent_find( R&& r, Pred pred = {}, Proj proj = {} );
(2) (since C++20)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S,
          class Proj = std::identity,
          std::indirect_binary_predicate
              <std::projected<I, Proj>,
               std::projected<I, Proj>> Pred = ranges::equal_to >
I adjacent_find( Ep&& policy, I first, S last,
                 Pred pred = {}, Proj proj = {} );
(3) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R,
          class Proj = std::identity,
          std::indirect_binary_predicate
              <std::projected<ranges::iterator_t<R>, Proj>,
               std::projected<ranges::iterator_t<R>, Proj>> Pred
              = ranges::equal_to >
ranges::borrowed_iterator_t<R>
    adjacent_find( Ep&& policy, R&& r, Pred pred = {}, Proj proj = {} );
(4) (since C++26)

For the definition of /*execution-policy*/, see this page; for the definition of /*sized-random-access-range*/, see this page.

Searches the source range for the first pair of adjacent elements (projected by proj) satisfying the given binary predicate pred.

1) The source range is [firstlast).
2) The source range is r.
3,4) Same as (1,2), but executed according to policy.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first, last - the iterator-sentinel pair defining the source range
r - the source range
pred - the predicate to be applied to the (projected) elements
proj - the projection to be applied to the elements
policy - the execution policy to use

Return value

The first iterator iter in the source range such that bool(std::invoke(pred, std::invoke(proj, *iter),
                 std::invoke(proj, *ranges::next(iter))))
evaluates to true.

If no such iterator is found, returns:

1,3) ranges::next(first, last)
2,4) ranges::next(ranges::begin(r), ranges::end(r))

Complexity

Given

  • result as the return value of adjacent_find,
  • M as ranges::distance(first, result) or ranges::distance(ranges::begin(r), result), and
  • N as ranges::distance(first, last) or ranges::distance(r):
1,2) Exactly min(M+1,N-1) applications of pred.
3,4) 𝓞(N) applications of pred.

proj is applied no more than double of the number of applications of pred.

Exceptions

3,4) During the execution process:
  • If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
  • If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).

Possible implementation

struct adjacent_find_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_binary_predicate
                 <std::projected<I, Proj>,
                  std::projected<I, Proj>> Pred = ranges::equal_to>
    constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const
    {
        if (first == last)
            return first;
        auto next = ranges::next(first);
        for (; next != last; ++next, ++first)
            if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *next)))
                return first;
        return next;
    }

    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_binary_predicate
                 <std::projected<ranges::iterator_t<R>, Proj>,
                  std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r, Pred pred = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r),
                       ranges::next(ranges::begin(r), ranges::end(r)),
                       std::ref(pred), std::ref(proj));
    }
};

inline constexpr adjacent_find_fn adjacent_find;

Example

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>

constexpr bool some_of(auto&& r, auto&& pred) // some but not all
{
    return std::ranges::cend(r) != std::ranges::adjacent_find(r,
        [&pred](const auto& x, const auto& y)
        {
            return pred(x) != pred(y);
        });
}

// test some_of
constexpr auto a = {0, 0, 0, 0}, b = {1, 1, 1, 0}, c = {1, 1, 1, 1};
auto is_one = [](auto x){ return x == 1; };
static_assert(!some_of(a, is_one) && some_of(b, is_one) && !some_of(c, is_one));

int main()
{
    const auto v = {0, 1, 2, 3, 40, 40, 41, 41, 5}; /*
                                ^^          ^^       */
    namespace ranges = std::ranges;
    
    if (auto it = ranges::adjacent_find(v.begin(), v.end()); it == v.end())
        std::cout << "No matching adjacent elements\n";
    else
        std::cout << "The first adjacent pair of equal elements is at ["
                  << ranges::distance(v.begin(), it) << "] == " << *it << '\n';
    
    if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.end())
        std::cout << "The entire vector is sorted in ascending order\n";
    else
        std::cout << "The last element in the non-decreasing subsequence is at ["
                  << ranges::distance(v.begin(), it) << "] == " << *it << '\n';
}

Output:

The first adjacent pair of equal elements is at [4] == 40
The last element in the non-decreasing subsequence is at [7] == 41

See also

finds the first two adjacent items that are equal (or satisfy a given predicate)
(function template) [edit]
removes consecutive duplicate elements in a range
(algorithm function object)[edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.