Namespaces
Variants

cpp/iterator/iterator: Difference between revisions

From cppreference.com
ugh, still a lot missing there, leaving a todo
Amirk (talk | contribs)
No edit summary
Line 58: Line 58:
         iterator(long _num = 0) : num(_num) {}
         iterator(long _num = 0) : num(_num) {}
         iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
         iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
         bool operator==(iterator other) const {return num == other.num;}
         bool operator==(iterator other) const {return num == other.num;}
         bool operator!=(iterator other) const {return !(*this == other);}
         bool operator!=(iterator other) const {return !(*this == other);}

Revision as of 14:41, 29 June 2016

 
 
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< 
    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.

Template parameters

Category - the category of the iterator. Must be one of iterator category tags.
T - the type of the values that can be obtained by dereferencing the iterator. This type should be void 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)

Member types

Member type Definition
iterator_category Category
value_type T
difference_type Distance
pointer Pointer
reference Reference

Example

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

#include <iostream>
#include <algorithm>

template<long FROM, long TO>
class Range {
public:
    // member typedefs provided through inheriting from std::iterator
    class iterator: public std::iterator<
                        std::forward_iterator_tag, // iterator_category
                        long,                      // value_type
                        long,                      // difference_type
                        const long*,               // pointer
                        const long&                // reference
                                      >{
        long num = FROM;
    public:
        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);}
        long operator*() {return num;}
    };
    iterator begin() {return FROM;}
    iterator end() {return TO >= FROM? TO+1 : TO-1;}
};

int main() {
    // std::find requires a input iterator
    auto 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 requirements
    for(long l : Range<3, 5>()) {
        std::cout << l << ' '; // 3 4 5
    }
    std::cout << '\n';
}

Output:

18
3 4 5


See also

provides uniform interface to the properties of an iterator
(class template) [edit]
empty class types used to indicate iterator categories
(class) [edit]