std::ranges...:filter_view(3)
π 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).
- A range adaptor that represents view of an underlying sequence without the elements that fail to satisfy a predicate.
RangeAdaptorObject. The expressionviews::filter(e, p)is expression-equivalent tofilter_view(e, p)for any suitable subexpressionseandp.
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 afilter_view(public member function)base(C++20) β returns the underlying viewV(public member function)pred(C++20) β returns a reference to the predicate stored withinfilter_view(public member function)begin(C++20) β returns the beginning iterator of thefilter_view(public member function)end(C++20) β returns the sentinel of thefilter_view(public member function)
Inherited from std::ranges::view_interface
empty(C++20) β returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (public member function ofstd::ranges::view_interface<D>)cbegin(C++23) β returns a constant iterator to the beginning of the range. (public member function ofstd::ranges::view_interface<D>)cend(C++23) β returns a sentinel for the constant iterator of the range. (public member function ofstd::ranges::view_interface<D>)operator bool(C++20) β returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (public member function ofstd::ranges::view_interface<D>)front(C++20) β returns the first element in the derived view. Provided if it satisfiesforward_range. (public member function ofstd::ranges::view_interface<D>)back(C++20) β returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (public member function ofstd::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) (since C++20)β Value-initializesbase_via its default member initializer (= V()) and default-initializespred_(which value-initializes the containedPred).(2) (since C++20)β Initializesbase_withstd::move(base)and initializespred_withstd::move(pred).
π Parameters π
baseβ range to filterpredβ predicate to filter out elements
π std::ranges::filter_view::base π
constexpr V base() const& requires std::copy_constructible<V>;
constexpr V base() &&;
(1) (since C++20)β Equivalent toreturn base_;.(2) (since C++20)β Equivalent toreturn 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 offilter_view;(exposition-only member class*).sentinel(C++20) β the sentinel type offilter_viewwhen the underlying view is not acommon_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 afilter_viewwhich does not contain aPred; correct behavior: ifPredis notdefault_initializable, thefilter_viewis also notdefault_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 returnsfalse;(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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms