zoukankan      html  css  js  c++  java
  • C++泛型编程(1)--自己实现C++迭代器/遍历器 iterator

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

  • 相关阅读:
    计算机操作系统 存储器管理
    数据结构 平衡二叉树avl c++
    数据结构 线索二叉树 c++
    数据结构 赫夫曼树及其应用 c++
    c++ cstring 常用函数
    数据结构 哈希表 c++
    数据结构 静态链表
    ajax返回填充的数据不显示
    使用JSON.parse()转化成json对象需要注意的地方
    参数错误导致bug
  • 原文地址:https://www.cnblogs.com/cv-pr/p/7765858.html
Copyright © 2011-2022 走看看