3.STL simple iterator implementation (including source code)

Source: Internet
Author: User

I use vs2015 to write the program (source download)

The main idea of the STL is to separate the containers (container) from the algorithms (algorithms), design them independently, and then bring them together with a stick, which is the iterator (iterator).

An iterator is a tool that accesses a container. Notice that there is a container before the tool that accesses the container. Iterators need to understand the characteristics of the container to be implemented, which determines that the iterator must go deep into the container, so STL simply gives the developer of the iterator to the container's designer.

The development of iterators is divided into three steps:

1. Design of elements;

2. Design of container for loading elements;

3. The design of the iterator that accesses the container;

For the sake of convenience, I designed an iterator based on a unidirectional list. Based on the development steps of the iterator, the design code of the linked list node is given first.


<pre name= "code" class= "CPP" > #ifndef _cgh_list_item_#define _cgh_list_item_//Define linked list node type Template<typename T >class listitem{public:         Tvalue () const{return _value;}         Listitem*next () const{return _next;}         ListItem (Tvalue): _value (value), _next (NULL) {}public:         t_value;         Listitem*_next;}; #endif

The list node consists of two member variables:

1. Save the _value of the node value;

2. _next pointer to the next node.

Next is the container design, I developed the container is a one-way list, one-way linked list class code as follows:

Template<typename t>class cghlist{public:         voidinsert_front (T value);  Insert node to list Head         voidinsert_end (T value);  Insert node to list trailing         voiddisplay (std::ostream& OS = std::cout) const;//Print List         cghlist (): _end (NULL), _front ( NULL) {}//constructor, initialization of the head pointer, tail pointer         listitem<t>*front () const{return _front;}//Return head pointer         listitem<t>*end () const{return _end;}//return tail pointer private:         listitem<t>*_end;  Chain-end pointer         listitem<t>*_front;  Long_size of the pointer of the linked table head         ;  Chain table Length};

The role of member variables and member functions of a one-way list is simple and is all basic list operations. The implementation of member functions is not complicated, mainly for the head and tail interpolation of the linked list. The implementation code for the member function is as follows.

Insert node to list header Template<typename t>void Cghlist<t>::insert_front (T value) {cghlist<t>::_size++;         listitem<t>*tmp = new listitem<t> (value);                   if (typename Cghlist<t>::_front = = NULL) {typenamecghlist<t>::_front = tmp;         Typenamecghlist<t>::_end = tmp;                   } else{tmp->_next= _front;         _front= tmp;         }}//Insert node to the tail of the list template<typename t>void cghlist<t>::insert_end (T value) {cghlist<t>::_size++;         listitem<t>*tmp = new listitem<t> (value);                   if (typename Cghlist<t>::_front = = NULL) {typenamecghlist<t>::_front = tmp;         Typenamecghlist<t>::_end = tmp;                   } else{typenamecghlist<t>::_end->_next = tmp;         Typenamecghlist<t>::_end = TypeName cghlist<t>::_end->_next; }}//Print List template<typename t>void cghlist<t>::d isplay (std::ostream&os = std::cout) const {if (typename Cghlist<t>::_front = = NULL | | TypeName cghlist<t>::_end ==null) {retur         N         } listitem<t>*tmp = TypeName cghlist<t>::_front;                   while (tmp! = NULL) {std::cout<< tmp->_value << Std::endl;         tmp= tmp->_next; }}

With the elements of containers and containers, the next most critical iterator is

On the code:


#ifndef _cgh_iterator_#define _cgh_iterator template<typename item>struct listiter:public std::iterator<std          :: forward_iterator_tag,item>{item*ptr;//container and Iterator link listiter (item*p = 0):p TR (p) {}//constructor, initialize PTR pointer item&operator* () const {return *ptr;}//Return container (This example is a linked list) node reference item*operator-> () const {return PTR; }//Returns the node address of the container (in this case the list)//points to the next node of the container (in this case, the list), i++ listiter&operator++ () {ptr= Ptr-&gt                   ; next ();         Return*this;                   }//point to the next node of the container (in this case, the list), ++i listiter&operator++ (int) {listitertmp = *this;                   ++*this;         returntmp;  }//To determine whether the two nodes are equal, that is, to determine whether the address of the two node is the same booloperator== (const listiter& i) const{returnptr = =         I.ptr; }//To determine whether the two nodes are unequal, that is, to determine whether the address of the two node is not the same booloperator!= (const listiter& i) const{returnptr         ! = I.ptr; }}; #endif

Iterators are defined directly with a struct, not class, why? Because the member variables and member functions of class are private access by default, the member variables and member functions of a struct are default to public access permissions. Designed as a struct means that anyone can use all the properties and functions of an iterator.

The only and most important member member variable of this iterator is the PTR pointer, which is actually the listitem* type and is the bridge that links the list and the iterator.

Next is the code for the Test Container:

Iterator.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "cghList.h" #include "cghIterator.h" #include "ListItem.h" #include <iostream> int _ Tmain (int argc, _tchar* argv[]) {         cghlist<int>mylist;//  define container                 //Add node to container for         (int i = 0; i < 5; i+ +) {                   mylist.insert_front (i);         }          Front-to-back print node         //mylist.display ();          for (listiter<listitem<int>> iter = Mylist.front (); ITER! = Mylist.end (); iter++) {                   std::cout<< Iter->_value << Std::endl;         }         System ("pause");         Return0;}

With this prototype, you can contribute to the inside, interested in children's shoes can be modified on this basis, write their own powerful iterator!

3.STL simple iterator implementation (including source code)

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.