std::ranges...:filter_view(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::ranges...:filter_view(3) πŸ’…

okay bestie, welcome to the C++ Standard Libary manual β€” yes, that's literally how it's spelled here β€” and we're about to make std::ranges::views::filter and std::ranges::filter_view make sense. no cap, this one is actually iconic.


✨ NAME ✨

std::ranges::views::filter,std::ranges::filter_view β€” literally std::ranges::views::filter,std::ranges::filter_view. that's the whole name, periodt.

πŸ¦‹ Synopsis πŸ¦‹

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 );
template< class Pred >
    constexpr /* range adaptor closure */ filter( Pred&& pred );

both (since C++20).

  1. A range adaptor that represents view of an underlying sequence without the elements that fail to satisfy a predicate.
  2. RangeAdaptorObject. The expression views::filter(e, p) is expression-equivalent 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. slay.

🌸 Member functions 🌸

  • 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 std::ranges::view_interface

  • empty (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>)
  • cbegin (C++23) β€” returns a constant iterator to the beginning of the range. (public member function of std::ranges::view_interface<D>)
  • cend (C++23) β€” returns a sentinel for the constant iterator of the range. (public member function of std::ranges::view_interface<D>)
  • operator bool (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>)
  • front (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>)
  • back (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>)

πŸŽ€ std::ranges::filter_view::filter_view πŸŽ€

filter_view() requires std::default_initializable<V> &&
             std::default_initializable<Pred> = default;
constexpr explicit filter_view( V base, Pred pred );
  1. (1) (since C++20) β€” Value-initializes base_ via its default member initializer (= V()) and default-initializes pred_ (which value-initializes the contained Pred).
  2. (2) (since C++20) β€” 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 πŸ’œ

constexpr V base() const& requires std::copy_constructible<V>;
constexpr V base() &&;
  1. (1) (since C++20) β€” Equivalent to return base_;.
  2. (2) (since C++20) β€” 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

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.

🧚 std::ranges::filter_view::end 🧚

constexpr auto end();

(since C++20)

Returns an iterator to the end. Equivalent to

if constexpr (ranges::common_range<V>)
    return /* iterator */{*this, ranges::end(base_)};
else
    return /* sentinel */{*this};

⭐ Deduction guides ⭐

template< class R, class Pred >
filter_view( R&&, Pred ) -> filter_view<views::all_t<R>, Pred>;

(since C++20)

πŸ‘‘ Nested classes πŸ‘‘

  • iterator (C++20) β€” the iterator type of filter_view; (exposition-only member class*).
  • sentinel (C++20) β€” the sentinel type of filter_view when the underlying view is not a common_range; (exposition-only member class*).

πŸ’– Example πŸ’–

// Run this code

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

Output

0 4 16

😭 Defect reports 😭

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • LWG 3714 (P2711R1), applied to C++20 β€” behavior as published: the multi-parameter constructor was not explicit; correct behavior: made explicit.
  • P2325R3, applied to C++20 β€” behavior as published: the default constructor constructs a filter_view which does not contain a Pred; correct behavior: if Pred is not default_initializable, the filter_view is also not default_initializable.

🫢 See also 🫢

  • ranges::take_while_view / views::take_while (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).

source: http://cppreference.com Β· version/date: 2024.06.10 β€” that's the footer, bestie, and you ate this page up πŸ’‹βœ¨

πŸ“¦ where this page comes from