1

I have a container (std::vector), and I'd like to check if any of its elements satisfy a predicate, to do that I use C++20's std::ranges::any_of.

The problem is, I'd like this any_of to skip cetain elements in the middle of the range (a sub-range with known offset and size). How can I "drop" those elements from the range before it goes into the any_of algorithm, while also not completely erasing them?

What I could think of is to use views::take and views::drop to get only the elements outside the subrange, then combining those into a new vector and views::joining it. But this seems rather inefficient, and definitely not very clean.
What would be a better way to achieve this result?

aradarbel10
  • 435
  • 3
  • 10
  • 2
    Just make two tanges and check them separately. – n. m. could be an AI Jul 24 '21 at 07:03
  • Any reason you need to use ranges just to check the elements of a vector? – Rlyeh Jul 24 '21 at 07:08
  • I did not think about checking the two separately, it's definitely an improvement over my idea. and the reason I use ranges is mostly for learning purposes, but also they seem a lot more convenient & safe than iterator pairs in general, so I don't see a reason to not use them. – aradarbel10 Jul 24 '21 at 07:11
  • 1
    @aradarbel10 You can create two [`subrange`](https://en.cppreference.com/w/cpp/ranges/subrange)s and use range-v3's [`views::concat`](https://github.com/ericniebler/range-v3/blob/master/test/view/concat.cpp) to combine them into one single range, then pass it to your `ranges::all_of`. – 康桓瑋 Jul 24 '21 at 07:31

0 Answers0