A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/iterator below:

class template

<iterator>

std::iterator
template <class Category,              // iterator::iterator_category          class T,                     // iterator::value_type          class Distance = ptrdiff_t,  // iterator::difference_type          class Pointer = T*,          // iterator::pointer          class Reference = T&         // iterator::reference          > class iterator;

Iterator base class

[Note: This page describes the base class std::iterator. For general information about iterators, refer to header]

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 instantiation automatically (and such instantiation is required to be valid for all iterator types).

It is defined as:


1
2
3
4
5
6
7
8
9
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:
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.

Member types member type definition notes iterator_category the first template parameter (Category) value_type the second template parameter (T) difference_type the third template parameter (Distance) defaults to: ptrdiff_t pointer the fourth template parameter (Pointer) defaults to: T* reference the fifth template parameter (Reference) defaults to: T&
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// std::iterator example
#include <iostream>     // std::cout
#include <iterator>     // std::iterator, std::input_iterator_tag

class MyIterator : public std::iterator<std::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) {MyIterator tmp(*this); operator++(); return tmp;}
  bool operator==(const MyIterator& rhs) const {return p==rhs.p;}
  bool operator!=(const MyIterator& rhs) const {return p!=rhs.p;}
  int& operator*() {return *p;}
};

int main () {
  int numbers[]={10,20,30,40,50};
  MyIterator from(numbers);
  MyIterator until(numbers+5);
  for (MyIterator it=from; it!=until; it++)
    std::cout << *it << ' ';
  std::cout << '\n';

  return 0;
}

Output:


See also
<iterator>
Iterator definitions (header)
iterator_traits
Iterator traits (class template)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4