Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::adjacent,std::ranges::adjacent_view,std::ranges::views::pairwise

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
      adjacent_viewviews::adjacent
      (C++23)(C++23)
      views::pairwise
      (C++23)

       
      std::ranges::adjacent_view
      Member functions
      Iterator
      Member functions
      Non-member functions
      Sentinel
      Member functions
      Non-member functions
       
      Defined in header<ranges>
      template<ranges::forward_range V,std::size_t N>

          requiresranges::view<V>&&(N>0)
      class adjacent_view

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

         template<std::size_t N>
         constexpr/* unspecified */ adjacent=/* unspecified */;

      }
      (2)(since C++23)
      namespace views{

         inlineconstexprauto pairwise= adjacent<2>;

      }
      (3)(since C++23)
      Call signature
      template<ranges::viewable_range R>

          requires/* see below */

      constexprranges::viewauto adjacent<N>( R&& r);
      (since C++23)
      1)adjacent_view is a range adaptor that takes aview, and produces aview whoseith element (a “window”) is astd::tuple that holdsN references to the elements[ii + N - 1] of the original view.
      LetS be the size of the original view. Then the size of produced view is:
      • S- N+1, ifS >= N,
      • 0 otherwise, and the resulting view is empty.
      2) The nameviews::adjacent<N> denotes aRangeAdaptorObject. Given a subexpressione and a constant expressionN, the expressionviews::adjacent<N>(e) isexpression-equivalent to
      3) The nameviews::pairwise denotes aRangeAdaptorObject that behaves exactly asviews::adjacent<2>.

      adjacent_view always modelsforward_range, and modelsbidirectional_range,random_access_range, orsized_range if adaptedview type models the corresponding concept.

      Contents

      [edit]Data members

      Member Description
      Vbase_ the underlyingview
      (exposition-only member object*)

      [edit]Member functions

      constructs aadjacent_view
      (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 the number of elements, provided only if the underlying (adapted) range satisfiessized_range
      (public member function)[edit]
      returns the approximate size of the resultingapproximately_sized_range
      (public member function)[edit]
      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]
      returns thenth element in the derived view, provided only if it satisfiesrandom_access_range
      (public member function ofstd::ranges::view_interface<D>)[edit]

      [edit]Deduction guides

      (none)

      [edit]Nested classes

      the iterator type
      (exposition-only member class template*)
      the sentinel type used whenadjacent_view is not acommon_range
      (exposition-only member class template*)

      [edit]Helper templates

      template<class V, size_t N>

      constexprboolranges::enable_borrowed_range<adjacent_view<V, N>>=

         ranges::enable_borrowed_range<V>;
      (since C++23)

      This specialization ofranges::enable_borrowed_range makesadjacent_view satisfyborrowed_range when the underlying view satisfies it.

      [edit]Notes

      views::adjacent only accepts forward ranges even whenN is0.

      There are similarities betweenranges::adjacent_view andranges::slide_view:

      • Both create “sliding window” of sizeN.
      • Both have the same sizeS - N + 1, whereS is the size of an adaptedview such thatS >= N > 0.

      The following table shows the differences between these adaptors:

      View adaptorvalue_typeThe window sizeN
      ranges::adjacent_viewstd::tupleA template parameter
      ranges::slide_viewranges::rangeA runtime argument
      Feature-test macroValueStdFeature
      __cpp_lib_ranges_zip202110L(C++23)ranges::zip_view,
      ranges::zip_transform_view,
      ranges::adjacent_view,
      ranges::adjacent_transform_view

      [edit]Example

      Run this code
      #include <array>#include <format>#include <iostream>#include <ranges>#include <tuple> int main(){constexprstd::array v{1,2,3,4,5,6};std::cout<<"v = [1 2 3 4 5 6]\n"; for(int i{};std::tuple t: v| std::views::adjacent<3>){auto[t0, t1, t2]= t;std::cout<<std::format("e = {:<{}}[{} {} {}]\n","",2* i++, t0, t1, t2);}}

      Output:

      v = [1 2 3 4 5 6]e = [1 2 3]e =   [2 3 4]e =     [3 4 5]e =       [4 5 6]

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 4098C++23views::adjacent<0> used to accept input-only rangesmade rejected

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 26.7.25 Adjacent view [range.adjacent]

      [edit]See also

      aview consisting of results of application of a transformation function to adjacent elements of the adapted view
      (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]
      a range ofviews that areN-sized non-overlapping successive chunks of the 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/adjacent_view&oldid=182453"

      [8]ページ先頭

      ©2009-2025 Movatter.jp