Defined in header <ranges>  

template< ranges::input_range V, std::indirect_unary_predicate<ranges::iterator_t<V>> Pred > requires ranges::view<V> && std::is_object_v<Pred> class filter_view : public ranges::view_interface<filter_view<V, Pred>>  (1)  (since C++20) 
namespace views { inline constexpr /* unspecified */ filter = /* unspecified */; }  (2)  (since C++20) 
Call signature  
template< ranges::viewable_range R, class Pred > requires /* see below */ constexpr ranges::view auto filter( R&& r, Pred&& pred );  (since C++20)  
template< class Pred > constexpr /*range adaptor closure*/ filter( Pred&& pred );  (since C++20) 
view
of an underlying sequence without the elements that fail to satisfy a predicate.views::filter(e, p)
is expressionequivalent to filter_view(e, p)
for any suitable subexpressions e
and p
.filter_view
models the concepts bidirectional_range
, forward_range
, input_range
, and common_range
when the underlying view
V
models respective concepts.
Expression e
is expressionequivalent to expression f
, if.
e
and f
have the same effects, and noexcept(e) == noexcept(f)
). Typical implementations of filter_view
hold two or three nonstatic data members:
view
of type V
(shown here as base_
for exposition only), base_
of type /*copyablebox*/<Pred>
(shown here as pred_
for exposition only), where copyablebox
is a wrapper class template that always satisfies copyable
, std::optional
like type (shown here as begin_
for exposition only) that caches an iterator to the first element of base_
that satisfies the pred_
. The begin_
may be present only if filter_view
models forward_range
. (constructor)
(C++20)  constructs a filter_view (public member function) 
base
(C++20)  returns the underlying view V (public member function) 
pred
(C++20)  returns a reference to the predicate stored within filter_view (public member function) 
begin
(C++20)  returns the beginning iterator of the filter_view (public member function) 
end
(C++20)  returns the sentinel of the filter_view (public member function) 
Inherited from 

(C++20)  Returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range . (public member function of std::ranges::view_interface<D> ) 
(C++20)  Returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (public member function of std::ranges::view_interface<D> ) 
(C++20)  Returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> ) 
(C++20)  Returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range . (public member function of std::ranges::view_interface<D> ) 
filter_view() requires std::default_initializable<V> && std::default_initializable<Pred> = default;  (1)  (since C++20) 
constexpr filter_view( V base, Pred pred );  (2)  (since C++20) 
base_
via its default member initializer (= V()
) and defaultinitializes pred_
(which valueinitializes the contained Pred
).base_
with std::move(base)
and initializes pred_
with std::move(pred)
.base    range to filter 
pred    predicate to filter out elements 
constexpr V base() const& requires std::copy_constructible<V>;  (1)  (since C++20) 
constexpr V base() &&;  (2)  (since C++20) 
return base_;
.return std::move(base_);
. constexpr const Pred& pred() const;  (since C++20) 
Returns a reference to the contained Pred
object. The behavior is undefined if pred_
does not contain a value.
constexpr /*iterator*/ begin();  (since C++20) 
In order to provide the amortized constant time complexity required by the range
concept, this function caches the result within the filter_view
object for use on subsequent calls. Equivalent to.
if constexpr (!ranges::forward_range<V>) { return /*iterator*/{*this, ranges::find_if(base_, std::ref(*pred_))}; } else { if (!begin_.has_value()) begin_ = ranges::find_if(base_, std::ref(*pred_)); // caching return /*iterator*/{*this, begin_.value())}; }
The behavior is undefined if pred_
does not contain a value.
constexpr auto end() { if constexpr (ranges::common_range<V>) return /*iterator*/{*this, ranges::end(base_)}; else return /*sentinel*/{*this}; }  (since C++20) 
template< class R, class Pred > filter_view( R&&, Pred ) > filter_view<views::all_t<R>, Pred>;  (since C++20) 
(C++20)  the iterator type of filter_view (expositiononly member class) 
(C++20)  the sentinel type of filter_view when the underlying view is not a common_range (expositiononly member class) 
#include <iostream> #include <ranges> int main() { auto even = [](int i) { return 0 == i % 2; }; auto square = [](int i) { return i * i; }; for (int i : std::views::iota(0, 6)  std::views::filter(even)  std::views::transform(square)) { std::cout << i << ' '; } }
Output:
0 4 16
The following behaviorchanging defect reports were applied retroactively to previously published C++ standards.
DR  Applied to  Behavior as published  Correct behavior 

P2325R3  C++20  if Pred is not default_initializable , the default constructorconstructs a filter_view which does not contain an Pred  the filter_view is alsonot default_initializable 
(C++20)  a view consisting of the initial elements of another view , until the first element on which a predicate returns false (class template) (range adaptor object) 
© cppreference.com
Licensed under the Creative Commons AttributionShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/ranges/filter_view