std::ranges::views::filter, std::ranges::filter_view - cppreference.com (original) (raw)

Defined in header
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)
  1. A range adaptor that represents view of an underlying sequence without the elements that fail to satisfy a predicate.

filter_view models the concepts bidirectional_range, forward_range, input_range, and common_range when the underlying view V models respective concepts.

Contents

[edit] Data members

Member Description
V base_ (private) the underlying view(exposition-only member object*)
copyable-box(until C++23)movable-box(since C++23) pred_ (private) wraps the predicate used to filter out elements of base_(exposition-only member object*)
non-propagating-cache<ranges::iterator_t<V>> begin_ (private) (present only if V satisfies forward_range) an object that caches an iterator to the first element of base_ that satisfies the pred_(exposition-only member object*)

[edit] Member functions

(constructor) constructs a filter_view (public member function)
base returns the underlying view V (public member function)
pred returns a reference to the predicate stored within filter_view (public member function)
begin returns the beginning iterator of the filter_view (public member function)
end returns the sentinel of the filter_view (public member function)
Inherited from std::ranges::view_interface
empty returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range (public member function of std::ranges::view_interface) [edit]
cbegin(C++23) returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit]
cend(C++23) returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit]
operator bool returns whether the derived view is not empty, provided only if ranges::empty is applicable to it (public member function of std::ranges::view_interface) [edit]
front returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit]
back returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit]

std::ranges::filter_view::filter_view

  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

| | (1) | (since C++20) | | | ---------------------- | ------------- | ------------- | | constexpr V base() &&; | (2) | (since C++20) |

  1. Equivalent to return base_;.

  2. Equivalent to return std::move(base_);.

std::ranges::filter_view::pred

| 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.

std::ranges::filter_view::begin

| constexpr /*iterator*/ begin(); | | (exposition only*) | | --------------------------------- | | ------------------- |

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

The behavior is undefined if _pred_ does not contain a value.

std::ranges::filter_view::end

| constexpr auto end(); | | (since C++20) | | --------------------- | | ------------- |

Returns an iterator to the end. Equivalent to

[edit] Deduction guides

| template< class R, class Pred >filter_view( R&&, Pred ) -> filter_view<views::all_t<R>, Pred>; | | (since C++20) | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ------------- |

[edit] 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 common_range(exposition-only member class*) |

[edit] Example

#include #include   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 << ' '; std::cout << '\n'; }

Output:

[edit] Defect reports

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 if Pred is not default_initializable, the default constructorconstructs a filter_view which does not contain a Pred the filter_view is alsonot default_initializable

[edit] See also