From cppreference.com
Defined in header
template<ranges::input_rangeV,std::indirect_unary_predicate<ranges::iterator_t<V>>Pred>requiresranges::view<V>&&std::is_object_v<Pred>classfilter_view:publicranges::view_interface<filter_view<V,Pred>> (1) (since C++20)namespaceviews{inlineconstexpr/* unspecified */filter=/* unspecified */;} (2) (since C++20)Call signature
template<ranges::viewable_rangeR,classPred>requires/* see below */constexprranges::viewautofilter(R&&r,Pred&&pred);(since C++20)template<classPred>constexpr/* range adaptor closure */filter(Pred&&pred);(since C++20)1) A range adaptor that represents a
of an underlying sequence without the elements that fail to satisfy a predicate.
filter_view models the concepts
,
,
, and
when the underlying
V models respective concepts.
Data members
Member Description Vbase_(private)the underlying view
(exposition-only member object*)pred_(private)wraps the predicate used to filter out elements of base_
(exposition-only member object*)
<ranges::iterator_t<V>>
begin_(private)
(present only if V satisfies
)an object that caches an iterator to the first element of base_ that satisfies the pred_
(exposition-only member object*)Member functions
constructs a filter_view
(public member function)
returns the underlying view V
(public member function)
returns a reference to the predicate stored within filter_view
(public member function)
returns the beginning iterator of the filter_view
(public member function)
returns the sentinel of the filter_view
(public member function) Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
std::ranges::filter_view::filter_view
filter_view()requiresstd::default_initializable<V>&&std::default_initializable<Pred>=default; (1) (since C++20)constexprexplicitfilter_view(Vbase,Predpred); (2) (since C++20)1) Value-initializes base_ via its default member initializer (=V()) and default-initializes pred_ (which value-initializes the contained Pred).
2) Initializes base_ with std::move(base) and initializes pred_ with std::move(pred).
Parameters
base - range to filter pred - predicate to filter out elements std::ranges::filter_view::base
constexprVbase()const&requiresstd::copy_constructible<V>; (1) (since C++20)constexprVbase()&&; (2) (since C++20)1) Equivalent to returnbase_;.
2) Equivalent to returnstd::move(base_);.
std::ranges::filter_view::pred
constexprconstPred&pred()const;(since C++20)Returns a reference to the contained Pred object. The behavior is undefined if pred_ does not contain a value.
std::ranges::filter_view::begin
constexpr/*iterator*/begin();(exposition only*)In order to provide the amortized constant time complexity required by the
concept, this function caches the result within the filter_view object for use on subsequent calls. Equivalent to
ifconstexpr(!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_));// cachingreturn/*iterator*/{*this,begin_.value())};}The behavior is undefined if pred_ does not contain a value.
std::ranges::filter_view::end
constexprautoend();(since C++20)Returns an iterator to the end. Equivalent to
ifconstexpr(ranges::common_range<V>)return/*iterator*/{*this,ranges::end(base_)};elsereturn/*sentinel*/{*this};Deduction guides
template<classR,classPred>filter_view(R&&,Pred)->filter_view<views::all_t<R>,Pred>;(since C++20)Nested classes
the iterator type of filter_view
(exposition-only member class*)the sentinel type of filter_view when the underlying view is not a
(exposition-only member class*)Example
Run this code
#include<iostream>#include<ranges>intmain(){autoeven=[](inti){return0==i%2;};autosquare=[](inti){returni*i;};for(inti:std::views::iota(0,6)|std::views::filter(even)|std::views::transform(square))std::cout<<i<<' ';std::cout<<'\n';}Output:
0 4 16 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
(
) C++20 the multi-parameter constructor was not explicit made explicit
C++20 if Pred is not
, the default constructor
constructs a filter_view which does not contain a Predthe filter_view is also
not
See also