C++迭代器/遍曆器 iterator實現

來源:互聯網
上載者:User

標籤:部落格   har   簡單的   mes   hive   uml圖   ace   ext   size   

1.原理

迭代器又稱為遍曆器,用於訪問容器中的資料,迭代器旨在演算法和容器之間搭建訪問的橋樑,從而使演算法和資料分離,不用關心資料具體的儲存細節。具體的原理描述請參考以下兩個部落格:

[1].C++迭代器 iterator

[2].Iterator模式C++實現

迭代器的UML圖:

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

2.實現

根據以上的原理圖,下面實現一個簡單的迭代器。

/* * 以下實現了一個容器的迭代器(訪問器) */#include <boost/assert.hpp>#include <iostream>using namespace std;// 迭代器基類template<typename T>class Iterater { public:  virtual ~Iterater() {  }  virtual void first() = 0;  virtual void next() = 0;  virtual bool isDone() = 0;  virtual T currentItem() = 0;};// 容器基類template<typename T>class Aggregate { public:  virtual ~Aggregate() {  }  virtual Iterater<T>* createIterater() = 0;  virtual int getSize() = 0;  virtual T getItem(int nIndex) = 0;};// 具體迭代器template<typename T>class ConcreateIterater : public Iterater<T> { private:  Aggregate<T>* p_;  int cur_index_; public:  ConcreateIterater(Aggregate<T>* agregate)      : cur_index_(0),        p_(agregate) {  }  ~ConcreateIterater() {  }  void first() {    cur_index_ = 0;  }  void next() {    if (cur_index_ < p_->getSize()) {      cur_index_++;    }  }  bool isDone() {    if (cur_index_ > p_->getSize() - 1) {      return true;    }    return false;  }  T currentItem() {    return p_->getItem(cur_index_);  }};// 具體迭代器template<typename T>class ConcreateAggregate : public Aggregate<T> { public:  ConcreateAggregate(int nSize)      : size_(nSize),        data_(NULL) {    data_ = new T[nSize];    for (int i = 0; i < nSize; i++) {      data_[i] = i;    }  }  Iterater<T>* createIterater() {    return new ConcreateIterater<T>(this);  }  int getSize() {    return size_;  }  T getItem(int nIndex) {    if (nIndex < 0 || nIndex >= size_)      return (T) (-1);    return data_[nIndex];  } public:  int size_;  T* data_;};int main(int argc, char** argv) {  Aggregate<double>* pag = new ConcreateAggregate<double>(10);  Iterater<double>* pcon = pag->createIterater();  // 1 of 2  //cxk::Iterater<int>* pcon = new cxk::ConcreateIterater<int>(pag); // 2 of 2  cout << "all value:" << endl;  for (pcon->first(); !pcon->isDone(); pcon->next()) {    cout << "value:" << pcon->currentItem() << endl;  }  return 0;}

 3.結果

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

 

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

C++迭代器/遍曆器 iterator實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.