In the following function
namespace r = std::ranges;
namespace rv = std::ranges::views;
std::optional<int> find_first_adjacent_duplicate(std::span<int> vals) {
auto pairs = vals | rv::slide(2);
auto adj_dup = r::find_if(pairs, [](auto&& r) {return r[0] == r[1]; });
if (adj_dup != pairs.end()) {
return (*adj_dup)[0];
} else {
return {};
}
}
The only reason I store vals | rv::slide(2)
in a local variable is because I need a name for the range view so that I can test the result of find_if
against its end iterator.
I was wondering if there is way to tell if adj_dup
is pairs.end()
just given adj_dup
?