Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::iterator

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      iterator
      (deprecated in C++17)


      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<

         class Category,
         class T,
         class Distance=std::ptrdiff_t,
         class Pointer= T*,
         class Reference= T&

      >struct iterator;
      (deprecated in C++17)

      std::iterator is the base class provided to simplify definitions of the required types for iterators.

      Contents

      [edit]Template parameters

      Category - the category of the iterator. Must be one ofiterator category tags.
      T - the type of the values that can be obtained by dereferencing the iterator. This type should bevoid for output iterators.
      Distance - a type that can be used to identify distance between iterators
      Pointer - defines a pointer to the type iterated over (T)
      Reference - defines a reference to the type iterated over (T)

      [edit]Member types

      Member type Definition
      iterator_categoryCategory
      value_typeT
      difference_typeDistance
      pointerPointer
      referenceReference

      [edit]Example

      The following example shows how to implement aninput iterator by inheriting from std::iterator

      Run this code
      #include <algorithm>#include <iostream> template<long FROM,long TO>class Range{public:// member typedefs provided through inheriting from std::iteratorclass iterator:public std::iterator<std::input_iterator_tag,// iterator_categorylong,// value_typelong,// difference_typeconstlong*,// pointerlong// reference>{long num= FROM;public:explicit iterator(long _num=0): num(_num){}        iterator& operator++(){ num= TO>= FROM? num+1: num-1;return*this;}        iterator operator++(int){ iterator retval=*this;++(*this);return retval;}bool operator==(iterator other)const{return num== other.num;}bool operator!=(iterator other)const{return!(*this== other);}        reference operator*()const{return num;}};    iterator begin(){return iterator(FROM);}    iterator end(){return iterator(TO>= FROM? TO+1: TO-1);}}; int main(){// std::find requires an input iteratorauto range= Range<15,25>();auto itr=std::find(range.begin(), range.end(),18);std::cout<<*itr<<'\n';// 18 // Range::iterator also satisfies range-based for requirementsfor(long l: Range<3,5>())std::cout<< l<<' ';// 3 4 5std::cout<<'\n';}

      Output:

      183 4 5

      [edit]See also

      provides uniform interface to the properties of an iterator
      (class template)[edit]
      empty class types used to indicate iterator categories
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/iterator&oldid=159921"

      [8]ページ先頭

      ©2009-2025 Movatter.jp