Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::views::counted

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      inlineconstexpr/*unspecified*/ counted=/*unspecified*/;
      (since C++20)
      Call signature
      template<class Iterator,class DifferenceType>

          requires/* see below */

      constexpr/*span-or-subrange*/ counted( Iterator&& it, DifferenceType&& count);
      (since C++20)

      A counted view presents aview of the elements of thecounted range[in) for some iteratori and non-negative integern.

      A counted range[in) is then elements starting with the element pointed to byi and up to but not including the element, if any, pointed to by the result ofn applications of++i.

      Ifn==0, the counted range is valid and empty. Otherwise, the counted range is only valid ifn is positive,i is dereferenceable, and[++i--n) is a valid counted range.

      Formally, ifit andcount are expressions,T isstd::decay_t<decltype((it))>, andD isstd::iter_difference_t<T>, then

      ifT modelsinput_or_output_iterator anddecltype((count)) modelsstd::convertible_to<D>,
      Otherwise,views::counted(it, count) is ill-formed.

      Contents

      Customization point objects

      The nameviews::counted denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.

      [edit]Notes

      views::counted does not check if the range is long enough to provide allcount elements: useviews::take if that check is necessary.

      [edit]Example

      Run this code
      #include <iostream>#include <ranges> int main(){constint a[]{1,2,3,4,5,6,7};for(int i: std::views::counted(a,3))std::cout<< i<<' ';std::cout<<'\n'; constauto il={1,2,3,4,5};for(int i: std::views::counted(il.begin()+1,3))std::cout<< i<<' ';std::cout<<'\n';}

      Output:

      1 2 32 3 4

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2393R1C++20implicit conversion from an integer-class type tostd::size_t might be invalidmade explicit

      [edit]See also

      aview consisting of the first N elements of anotherview
      (class template)(range adaptor object)[edit]
      combines an iterator-sentinel pair into aview
      (class template)[edit]
      iterator adaptor that tracks the distance to the end of the range
      (class template)[edit]
      returns the number of elements satisfying specific criteria
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/view_counted&oldid=160989"

      [8]ページ先頭

      ©2009-2025 Movatter.jp