On this page
std::ranges::views::filter, std::ranges::filter_view
Defined in header <ranges> |
||
---|---|---|
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Call signature | ||
|
(since C++20) | |
|
(since C++20) |
view
of an underlying sequence without the elements that fail to satisfy a predicate.
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.
Data members
Member name | Definition |
---|---|
base_ (private) |
The underlying view of type V .(exposition-only member object*) |
pred_ (private) |
Wraps the predicate used to filter out elements of base_ of type copyable-box <Pred> (until C++23)movable-box <Pred> (since C++23) augmenting Pred with assignability when needed and hence always satisfies copyable or movable (since C++23).(exposition-only member object*) |
begin_ (private) (optional) |
An object of optional-like type that caches an iterator to the first element of base_ that satisfies the pred_ . Present only if filter_view models forward_range .(exposition-only member object*) |
Member functions
(C++20)
|
constructs a filter_view (public member function) |
(C++20)
|
returns the underlying view V (public member function) |
(C++20)
|
returns a reference to the predicate stored within filter_view (public member function) |
(C++20)
|
returns the beginning iterator of the filter_view (public member function) |
(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++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> ) |
(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> ) |
std::ranges::filter_view::filter_view
|
(1) | (since C++20) |
|
(2) | (since C++20) |
base_
via its default member initializer (= V()
) and default-initializes pred_
(which value-initializes the contained Pred
).
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) |
|
(2) | (since C++20) |
return base_;
.
return std::move(base_);
.
std::ranges::filter_view::pred
|
(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
|
(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
|
(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
|
(since C++20) |
Nested classes
(C++20)
|
the iterator type of filter_view (exposition-only member class*) |
(C++20)
|
the sentinel type of filter_view when the underlying view is not a common_range (exposition-only member class*) |
Example
#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.
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 |
See also
(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 Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/ranges/filter_view