Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::counted_iterator

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       
       
      Defined in header<iterator>
      template<std::input_or_output_iterator I>
      class counted_iterator;
      (since C++20)

      std::counted_iterator is an iterator adaptor which behaves exactly like the underlying iterator, except that it keeps track of the distance to the end of its range. This iterator is equal tostd::default_sentinel if and only if its count reaches zero.

      Contents

      [edit]Member types

      Member type Definition
      iterator_typeI
      value_type
      (conditionally present)
      std::iter_value_t<I> ifI modelsindirectly_readable; otherwise, not defined
      difference_typestd::iter_difference_t<I>
      iterator_concept
      (conditionally present)
      I::iterator_concept if present; otherwise, not defined
      iterator_category
      (conditionally present)
      I::iterator_category if present; otherwise, not defined

      [edit]Member objects

      Member name Definition
      current(private) the underlying iterator whichbase() accesses
      (exposition-only member object*)
      length(private) the distance between the underlying iterator and the end of its range
      (exposition-only member object*)

      [edit]Member functions

      constructs a newcounted_iterator
      (public member function)[edit]
      assigns anothercounted_iterator
      (public member function)[edit]
      accesses the underlying iterator
      (public member function)[edit]
      returns the distance to the end
      (public member function)[edit]
      accesses the pointed-to element
      (public member function)[edit]
      accesses an element by index
      (public member function)[edit]
      advances or decrements thecounted_iterator
      (public member function)[edit]

      [edit]Non-member functions

      compares the distances to the end
      (function template)[edit]
      checks if the distance to the end is equal to​0​
      (function template)[edit]
      (C++20)
      advances the iterator
      (function template)[edit]
      (C++20)
      computes the distance between two iterator adaptors
      (function template)[edit]
      computes the signed distance to the end
      (function template)[edit]
      (C++20)
      casts the result of dereferencing the underlying iterator to its associated rvalue reference type
      (function)[edit]
      (C++20)
      swaps the objects pointed to by two underlying iterators
      (function template)[edit]

      [edit]Helper classes

      provides uniform interface to the properties of thestd::counted_iterator type
      (class template specialization)[edit]

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <string>#include <vector> using std::operator""s; void print(autoconst remark,autoconst& v){constauto size=std::ssize(v);std::cout<< remark<<'['<< size<<"] { ";for(auto it= std::counted_iterator{std::cbegin(v), size};         it!=std::default_sentinel;++it)std::cout<<*it<<(it.count()>1?", ":" ");std::cout<<"}\n";} int main(){constauto src={"Arcturus"s,"Betelgeuse"s,"Canopus"s,"Deneb"s,"Elnath"s};    print("src", src);std::vector<decltype(src)::value_type> dst;    std::ranges::copy(std::counted_iterator{src.begin(),3},std::default_sentinel,std::back_inserter(dst));    print("dst", dst);}

      Output:

      src[5] { Arcturus, Betelgeuse, Canopus, Deneb, Elnath }dst[3] { Arcturus, Betelgeuse, Canopus }

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2259R1C++20member typedefs are not providedstd::incrementable_traits
      is specialized forcounted_iterator
      member typedefs are added to account foriterator_traits fix
      redundantstd::incrementable_traits specialization is removed

      [edit]See also

      default sentinel for use with iterators that know the bound of their range
      (class)[edit]
      creates a subrange from an iterator and a count
      (customization point object)[edit]
      aview consisting of the first N elements of anotherview
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/counted_iterator&oldid=173507"

      [8]ページ先頭

      ©2009-2025 Movatter.jp