C + + iterator pattern implementation

Source: Internet
Author: User

There are a number of iterator implementations in the STL template library that isolate the algorithm implementation and the Access interface, and we can write our own iterators. Iterators in STL are inherited to a common iterator interface:

Template <class _category, class _tp, class _distance = ptrdiff_t,          class _pointer = _tp*, class _reference = _tp&am P;>struct iterator {  typedef _category  iterator_category;//iterator type  typedef _tp        value_type;  typedef _distance  Difference_type;  typedef _pointer   Pointer;  typedef _reference Reference;};

An iterator type is used to indicate the label of an instantiated iterator, such as an input iterator (write), an output iterator (read), a random-access iterator, and so on, and the algorithm can be implemented in various versions by different iterator types.

The __distance algorithm has different implementations for different iterators:

Template <class _inputiterator, class _distance>inline void __distance (_inputiterator __first, _inputiterator __ Last,                       _distance& __n, Input_iterator_tag) {while  (__first! = __last) {++__first; ++__n;}} Template <class _randomaccessiterator, class _distance>inline void __distance (_randomaccessiterator __first,                        _randomaccessiterator __last,                        _distance& __n, Random_access_iterator_tag) {  __stl_requires (_ Randomaccessiterator, _randomaccessiterator);  __n + = __last-__first;}

All of the iterator types are declared as follows:

struct Input_iterator_tag {};struct Output_iterator_tag {};struct forward_iterator_tag:public input_iterator_tag {}; struct Bidirectional_iterator_tag:public Forward_iterator_tag {};struct random_access_iterator_tag:public Bidirectional_iterator_tag {};

The important methods of iterators are as follows:

t& operator* () const t* operator-> () const BOOL operator== (const iterator & __x) Constbool operator!= (const ITE Rator & __x) const iterator& operator++ () iterator operator++ (int)

This allows you to implement your own iterators:

#include <iostream> #include <iterator>using namespace std;template <typename t>struct _a_node {_a_ Node *next;_a_node *prev; T data;}; Template <typename t>class a_iterator:public iterator<forward_iterator_tag,t>{private:typedef A_ Iterator<t> self;_a_node<t> *_node;void incr () {_node = _node->next;} void Decr () {_node = (_a_node<t>*) _node->prev;} Public:a_iterator (): _node (0) {}a_iterator (_a_node<t> *x): _node (x) {}~a_iterator () {}t& operator* () const { return _node->data;} t* operator-> () const {return & (operator* ());} BOOL operator== (const self& __x) Const {return _node = = __x._node;} BOOL Operator!= (const self& __x) const {return _node! = __x._node;} self& operator++ () {incr (); return *this;} Self operator++ (int.) {Self __tmp = *this; incr (); return __tmp;} self& operator--() {DECR (); return *this;} Self operator--(int) {self __tmp = *this; Decr (); return __tmp;}}; Template <typename t>class A {Private:typEdef T node;_a_node<t> *pnode;public:typedef a_iterator<t> iterator;typedef _A_node<T> _Node; A () {pnode = new _a_node<t>;p node->next = Pnode;pnode->prev = Pnode;} ~a () {//add delete node function heredelete Pnode;} Iterator begin () {return (_node *) (pnode->next);} Iterator End () {return pnode;} Method/////////////////////void push (T value) {_a_node<t> *v = new _a_node<t>;v->data = Value;v->next = Pnode->next;pnode->next->prev = V;pnode->next = V;v->prev = PNode;} T pop () {t Value;_node *next = Pnode->next;pnode->next = Next->next;next->prev = Pnode;next->prev = Next;ne Xt->next = Next;value = Next->data;delete Next;return value;}}; int main (int argc, char *argv[]) {a<int> A;a.push (1); A.push (2); A.pop (); for (A<int>::iterator iter = A.begin ( ); ITER! = A.end (); iter++) {*iter = 3;std::cout << "value:" << *iter << Std::endl;} int b = A.pop (); Std::cout << (A.begiN () = = A.end ()) << ", B:" << b << std::endl;return 0;} 

  

C + + iterator pattern implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.