cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : iterator : iterator
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
iterator
advance
back_inserter
distance
front_inserter
inserter
iterator
iterator_traits
iterator categories:
· BidirectionalIterator
· ForwardIterator
· InputIterator
· OutputIterator
· RandomAccessIterator
predefined iterators:
· back_insert_iterator
· front_insert_iterator
· insert_iterator
· istreambuf_iterator
· istream_iterator
· ostreambuf_iterator
· ostream_iterator
· reverse_iterator

-

iterator class template
<iterator>

Iterator base class

This is a base class template that can be used to derive iterator classes from it. It is not an iterator class and does not provide any of the functionality an iterator is expected to have.

This base class only provides some member types, which in fact are not required to be present in any iterator type (iterator types have no specific member requirements), but they might be useful, since they define the members needed for the default iterator_traits class template to generate the appropriate iterator_traits class automatically.

It is defined as:

template <class Category, class T, class Distance = ptrdiff_t,
          class Pointer = T*, class Reference = T&>
  struct iterator {
    typedef T         value_type;
    typedef Distance  difference_type;
    typedef Pointer   pointer;
    typedef Reference reference;
    typedef Category  iterator_category;
  };

Template parameters

Category
Category to which the iterator belongs to. It must be one of the following iterator tags:
iterator tagCategory of iterators
input_iterator_tagInput Iterator
output_iterator_tagOutput Iterator
forward_iterator_tagForward Iterator
bidirectional_iterator_tagBidirectional Iterator
random_access_iterator_tagRandom Access Iterator
T
Type of elements pointed by the iterator.
Distance
Type to represent the difference between two iterators.
Pointer
Type to represent a pointer to an element pointed by the iterator.
Reference
Type to represent a reference to an element pointed by the iterator.

Example

// iterator example
#include <iostream>
#include <iterator>
using namespace std;

class myiterator : public iterator<input_iterator_tag, int>
{
  int* p;
public:
  myiterator(int* x) :p(x) {}
  myiterator(const myiterator& mit) : p(mit.p) {}
  myiterator& operator++() {++p;return *this;}
  myiterator& operator++(int) {p++;return *this;}
  bool operator==(const myiterator& rhs) {return p==rhs.p;}
  bool operator!=(const myiterator& rhs) {return p!=rhs.p;}
  int& operator*() {return *p;}
};

int main () {
  int numbers[]={10,20,30,40,50};
  myiterator beginning(numbers);
  myiterator end(numbers+5);
  for (myiterator it=beginning; it!=end; it++)
	  cout << *it << " ";
  cout << endl;

  return 0;
}

Output


10 20 30 40 50

See also

iterator Iterator definitions (header)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us