|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
template<ranges::input_range V, std::indirect_unary_predicate<ranges::iterator_t<V>> Pred> | (1) | (since C++20) |
namespace views{ inlineconstexpr/* unspecified */ filter=/* unspecified */; | (2) | (since C++20) |
Call signature | ||
template<ranges::viewable_range R,class Pred> requires/* see below */ | (since C++20) | |
template<class Pred> constexpr/* range adaptor closure */ filter( Pred&& pred); | (since C++20) | |
view
of an underlying sequence with only the elements that satisfy the predicate.filter_view
models the conceptsbidirectional_range
,forward_range
,input_range
, andcommon_range
when the underlyingview
V
models respective concepts.
Contents |
Member | Description |
V base_ | the underlying view (exposition-only member object*) |
copyable-box <Pred> (until C++23)movable-box <Pred> (since C++23)pred_ | wraps the predicate used to filter out elements ofbase_ (exposition-only member object*) |
non-propagating-cache<ranges::iterator_t<V>>begin_ (present only if V satisfiesforward_range ) | an object that caches an iterator to the first element ofbase_ that satisfies thepred_ (exposition-only member object*) |
constructs afilter_view (public member function) | |
returns the underlying viewV (public member function) | |
returns a reference to the predicate stored withinfilter_view (public member function) | |
returns the beginning iterator of thefilter_view (public member function) | |
returns the sentinel of thefilter_view (public member function) | |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D> )[edit] |
(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D> )[edit] |
returns whether the derived view is not empty, provided only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D> )[edit] | |
returns the first element in the derived view, provided if it satisfiesforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range (public member function of std::ranges::view_interface<D> )[edit] |
filter_view() requiresstd::default_initializable<V>&& std::default_initializable<Pred>=default; | (1) | (since C++20) |
constexprexplicit filter_view( V base, Pred pred); | (2) | (since C++20) |
base_
via its default member initializer (= V()) and default-initializespred_
(which value-initializes the containedPred
).base_
withstd::move(base) and initializespred_
withstd::move(pred).base | - | range to filter |
pred | - | predicate to filter out elements |
constexpr V base()const& requiresstd::copy_constructible<V>; | (1) | (since C++20) |
constexpr V base()&&; | (2) | (since C++20) |
constexprconst Pred& pred()const; | (since C++20) | |
Returns a reference to the containedPred
object. The behavior is undefined ifpred_
does not contain a value.
constexpr/*iterator*/ begin(); | (exposition only*) | |
In order to provide the amortized constant time complexity required by therange
concept, this function caches the result within thefilter_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 ifpred_
does not contain a value.
constexprauto end(); | (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};
template<class R,class Pred> filter_view( R&&, Pred)-> filter_view<views::all_t<R>, Pred>; | (since C++20) | |
the iterator type offilter_view (exposition-only member class*) | |
the sentinel type offilter_view when the underlying view is not acommon_range (exposition-only member class*) |
#include <iostream>#include <ranges> int main(){auto even=[](int i){return0== 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<<' ';std::cout<<'\n';}
Output:
0 4 16
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3714 (P2711R1) | C++20 | the multi-parameter constructor was not explicit | made explicit |
P2325R3 | C++20 | ifPred is notdefault_initializable , the default constructorconstructs a filter_view which does not contain aPred | thefilter_view is alsonot default_initializable |
aview consisting of the initial elements of anotherview , until the first element on which a predicate returnsfalse(class template)(range adaptor object)[edit] |