C + + iterator/Walker iterator implementation

Source: Internet
Author: User

1. Principle

An iterator, also called a walker, is used to access the data in the container, which is designed to bridge the access between the algorithm and the container, thus separating the algorithm from the data, without worrying about the specific storage details of the data. Please refer to the following two blogs for a detailed description of the principle:

[1]. C + + iterator iterator

[2]. iterator Mode C + + implementation

UML diagram for iterators:

(From: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/25/1764934.html)

2. Implement

Based on the above schematic diagram, a simple iterator is implemented below.

/** The following implements a container iterator (accessor)*/#include<boost/assert.hpp>#include<iostream>using namespacestd;//iterator base classTemplate<typename t>classIterater { Public:  Virtual~Iterater () {}Virtual voidFirst () =0; Virtual voidNext () =0; Virtual BOOLIsDone () =0; VirtualT CurrentItem () =0;};//container base classTemplate<typename t>classAggregate { Public:  Virtual~Aggregate () {}Virtualiterater<t>* createiterater () =0; Virtual intGetSize () =0; VirtualT GetItem (intNIndex) =0;};//Specific iteratorsTemplate<typename t>classConcreateiterater: PublicIterater<t> { Private: Aggregate<t>*P_; intcur_index_; Public: Concreateiterater (Aggregate<t>*agregate): Cur_index_ (0), P_ (agregate) {}~Concreateiterater () {}voidFirst () {cur_index_=0; }  voidNext () {if(Cur_index_ < p_->GetSize ()) {Cur_index_++; }  }  BOOLIsDone () {if(Cur_index_ > P_->getsize ()-1) {      return true; }    return false; } T CurrentItem () {returnP_->GetItem (cur_index_); }};//Specific iteratorsTemplate<typename t>classConcreateaggregate: PublicAggregate<t> {  Public: Concreateaggregate (intnSize): Size_ (NSize), Data_ (NULL) {Data_=NewT[nsize];  for(inti =0; i < nSize; i++) {Data_[i]=i; }} iterater<t>*Createiterater () {return NewConcreateiterater<t> ( This); }  intGetSize () {returnSize_; } T GetItem (intNIndex) {    if(NIndex <0|| NIndex >=size_)return(T) (-1); returnData_[nindex]; }  Public:  intSize_; T*Data_;};intMainintargcChar**argv) {Aggregate<Double>* Pag =Newconcreateaggregate<Double> (Ten); Iterater<Double>* Pcon = Pag->createiterater ();//1 of 2//cxk::iterater<int>* Pcon = new cxk::concreateiterater<int> (PAG);//2 of 2cout <<"All value:"<<Endl;  for(Pcon->first ();!pcon->isdone (); pcon->Next ()) {cout<<"Value:"<< Pcon->currentitem () <<Endl; }  return 0;}

3. Results

All Value:value: 0 Value: 1 Value: 2 Value: 3 Value: 4 Value: 5 Value: 6 Value: 7 Value: 8 Value: 9

All Value:value:0value:1value:2value:3value:4value:5value:6value:7value:8value:9

C + + iterator/Walker iterator 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.