Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::filter,std::ranges::filter_view

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
       
      Defined in header<ranges>
      template<ranges::input_range V,

               std::indirect_unary_predicate<ranges::iterator_t<V>> Pred>
          requiresranges::view<V>&&std::is_object_v<Pred>
      class filter_view

         :publicranges::view_interface<filter_view<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 */

      constexprranges::viewauto 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 representsview of an underlying sequence with only the elements that satisfy the predicate.
      2)RangeAdaptorObject. The expressionviews::filter(e, p) isexpression-equivalent tofilter_view(e, p) for any suitable subexpressionse andp.

      filter_view models the conceptsbidirectional_range,forward_range,input_range, andcommon_range when the underlyingviewV models respective concepts.

      Contents

      [edit]Data members

      Member Description
      Vbase_ 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 ifV satisfiesforward_range)
      an object that caches an iterator to the first element ofbase_ that satisfies thepred_
      (exposition-only member object*)

      [edit]Member functions

      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 ofstd::ranges::view_interface<D>)[edit]
      (C++23)
      returns a constant iterator to the beginning of the range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      (C++23)
      returns a sentinel for the constant iterator of the range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns whether the derived view is not empty, provided only ifranges::empty is applicable to it
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns the first element in the derived view, provided if it satisfiesforward_range
      (public member function ofstd::ranges::view_interface<D>)[edit]
      returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range
      (public member function ofstd::ranges::view_interface<D>)[edit]

      std::ranges::filter_view::filter_view

      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)
      1) Value-initializesbase_ via its default member initializer (= V()) and default-initializespred_ (which value-initializes the containedPred).
      2) Initializesbase_ withstd::move(base) and initializespred_ withstd::move(pred).

      Parameters

      base - range to filter
      pred - predicate to filter out elements

      std::ranges::filter_view::base

      constexpr V base()const& requiresstd::copy_constructible<V>;
      (1)(since C++20)
      constexpr V base()&&;
      (2)(since C++20)
      1) Equivalent toreturn base_;.
      2) Equivalent toreturn std::move(base_);.

      std::ranges::filter_view::pred

      constexprconst Pred& pred()const;
      (since C++20)

      Returns a reference to the containedPred object. The behavior is undefined ifpred_ 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 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.

      std::ranges::filter_view::end

      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};

      [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 offilter_view
      (exposition-only member class*)
      the sentinel type offilter_view when the underlying view is not acommon_range
      (exposition-only member class*)

      [edit]Example

      Run this code
      #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

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3714
      (P2711R1)
      C++20the multi-parameter constructor was not explicitmade explicit
      P2325R3C++20ifPred is notdefault_initializable, the default constructor
      constructs afilter_view which does not contain aPred
      thefilter_view is also
      notdefault_initializable

      [edit]See also

      aview consisting of the initial elements of anotherview, until the first element on which a predicate returnsfalse
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/filter_view&oldid=183296"

      [8]ページ先頭

      ©2009-2025 Movatter.jp