Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::chunk_by,std::ranges::chunk_by_view

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

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

         :publicranges::view_interface<chunk_by_view<V, Pred>>
      (1)(since C++23)
      namespace views{

         inlineconstexpr/* unspecified */ chunk_by=/* unspecified */;

      }
      (2)(since C++23)
      Call signature
      template<ranges::viewable_range R,class Pred>

          requires/* see below */

      constexprranges::viewauto chunk_by( R&& r, Pred&& pred);
      (since C++23)
      template<class Pred>
      constexpr/*range adaptor closure*/ chunk_by( Pred&& pred);
      (since C++23)
      1)chunk_by_view is a range adaptor that takes aview and an invocable objectpred (the binary predicate), and produces aview of subranges (chunks), by splitting the underlying view between each pair of adjacent elements for whichpred returnsfalse. The first element of each such pair belongs to the previous chunk, and the second element belongs to the next chunk.
      2) The nameviews::chunk_by denotes aRangeAdaptorObject. Given a subexpressione andf, the expressionviews::chunk_by(e, f) isexpression-equivalent tochunk_by_view(e, f).

      chunk_by_view always modelsforward_range, and modelsbidirectional_range and/orcommon_range, if adaptedview type models the corresponding concepts.chunk_by_view never modelsborrowed_range orsized_range.

      Contents

      [edit]Data members

      Member Definition
      Vbase_ the underlyingview
      (exposition-only member object*)
      movable-box<Pred>pred_ an object that wraps the predicate used to split the elements ofbase_
      (exposition-only member object*)
      non-propagating-cache<iterator>begin_ an object that caches the iterator to the first element
      (exposition-only member object*)

      [edit]Member functions

      constructs achunk_by_view
      (public member function)[edit]
      returns a copy of the underlying (adapted) view
      (public member function)[edit]
      returns a reference to the stored predicate
      (public member function)[edit]
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator or a sentinel to the end
      (public member function)[edit]
      returns an iterator to the begin of the next subrange
      (exposition-only member function*)
      returns an iterator to the begin of the previous subrange
      (exposition-only 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]

      [edit]Deduction guides

      [edit]Nested classes

      the iterator type
      (exposition-only member class template*)

      [edit]Notes

      In order to provide the amortized constant time complexity required by therange concept, the result ofbegin() is cached within thechunk_by_view object. If the underlying range is modified after the first call tobegin(), subsequent uses of thechunk_by_view object might have unintuitive behavior.

      Feature-test macroValueStdFeature
      __cpp_lib_ranges_chunk_by202202L(C++23)std::ranges::chunk_by_view

      [edit]Example

      Run this code
      #include <functional>#include <iostream>#include <ranges>#include <string_view> void print_chunks(auto view,std::string_view separator=", "){for(autoconst subrange: view){std::cout<<'[';for(std::string_view prefix;autoconst& elem: subrange)std::cout<< prefix<< elem, prefix= separator;std::cout<<"] ";}std::cout<<'\n';} int main(){std::initializer_list v1={1,2,3,1,2,3,3,3,1,2,3};auto fn1= std::ranges::less{};auto view1= v1| std::views::chunk_by(fn1);    print_chunks(view1); std::initializer_list v2={1,2,3,4,4,0,2,3,3,3,2,1};auto fn2= std::ranges::not_equal_to{};auto view2= v2| std::views::chunk_by(fn2);    print_chunks(view2); std::string_view v3="__cpp_lib_ranges_chunk_by";auto fn3=[](auto x,auto y){return not(x=='_' or y=='_');};auto view3= v3| std::views::chunk_by(fn3);    print_chunks(view3,""); std::string_view v4="\u007a\u00df\u6c34\u{1f34c}";// "zß水🍌"auto fn4=[](auto,auto ß){return128==((128+64)& ß);};auto view4= v4| std::views::chunk_by(fn4);    print_chunks(view4,"");}

      Output:

      [1, 2, 3] [1, 2, 3] [3] [3] [1, 2, 3] [1, 2, 3, 4] [4, 0, 2, 3] [3] [3, 2, 1] [_] [_] [cpp] [_] [lib] [_] [ranges] [_] [chunk] [_] [by][z] [ß] [水] [🍌]

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 26.7.30 Chunk by view [range.chunk.by]

      [edit]See also

      a range ofviews that areN-sized non-overlapping successive chunks of the elements of anotherview
      (class template)(range adaptor object)[edit]
      aview whose Mth element is aview over the Mth through (M + N - 1)th elements of anotherview
      (class template)(range adaptor object)[edit]
      aview consisting of elements of anotherview, advancing over N elements at a time
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/chunk_by_view&oldid=176938"

      [8]ページ先頭

      ©2009-2025 Movatter.jp