NoFear

导航

简单实现一个迭代器

Posted on 2012-09-14 10:32  Fear_Hao  阅读(507)  评论(2编辑  收藏  举报

1  迭代器简单概念

 

迭代器:一个能遍历容器所有元素的智能指针

 

2  迭代器设计理念

 

STL的中心思想是:容器和算法分开(通过C++的class templates和function templates),彼此独立设计,最后用迭代器把他们粘合起来。

 

3  简单模拟代码【参考STL源码解析83页,可以编译通过】

 

//File:mylist.h 一个模板链表

#include <iostream>
using namespace std;

template <typename T>
class ListNode
{

public:
    ListNode(T value,ListNode* next = NULL):m_value(0),m_next(NULL)
    {
        m_value = value;
        m_next = next;
    }

    void set_value(T value){ m_value = value;}
    T get_value() const { return m_value; }

    ListNode* get_next() const { return m_next; }
    void set_next(ListNode *next){ m_next = next; }

private:
        T m_value;
        ListNode* m_next;
};

template <typename T>
class List
{
public:

    List():m_front(NULL),m_end(NULL),m_size(0){}

    //头插
    void insert_front(T value)
    {
        if(m_front == NULL)
        {
            m_end = m_front = new ListNode<T>(value,NULL);
            return;
        }
    
        m_front = new ListNode<T>(value,m_front);
        m_size++;
    }

    //尾插
    void insert_end(T value)
    {
        if(m_end == NULL)
        {
             m_end = m_front = new ListNode<T>(value,NULL);
            return;
        }
        
        ListNode<T>* tmp = new ListNode<T>(value,NULL);
        m_end->set_next(tmp);
        m_end = tmp;
        m_size++;
    }

    void display(ostream &os = cout) const
    {
        ListNode<T> * tmp = m_front;
        while(tmp != NULL)
        {
            os<<tmp->get_value()<<" ";
            tmp = tmp->get_next();
        }
        os<<endl;
    }

    ListNode<T>* get_front() const{ return m_front; }
    ListNode<T>* get_end() const{ return m_end; }

private:
    ListNode<T> *m_front;
    ListNode<T> *m_end;
    long m_size;
};

 

 

//File:mylist_iter.h 迭代器

#include "mylist.h"

template <class Item>
struct ListIter
{
    Item *ptr;

    //默认构造
    ListIter(Item *p = 0):ptr(p){}

    //拷贝构造和=拷贝赋值不需要 浅拷贝足够了

    Item& operator*() const { return *ptr; }
    Item* operator->() const { return ptr; }

    //pre-increment
    ListIter& operator++()
    {
        ptr = ptr->get_next();
        return *this;
    }

    //post-increment
    ListIter operator++(int)
    {
        ListIter tmp = *this;
        ++*this;
        return tmp;
    }

    bool operator ==(const ListIter& I) const{ return ptr == I.ptr; }

    bool operator !=(const ListIter& I) const{ return ptr != I.ptr; }
};

 

 

 

//File:main.cpp

#include "mylist_iter.h" //为下面*first != value 重载!= template <typename T> bool operator != (const ListNode<T>& node,T n) { return node.get_value() != n; } //算法find()独立出来 接口为迭代器 和容器分离 template <class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& value) { while(first != last && *first != value) ++first; return first; } int main() { List<int> mylist; //链表初始化 for(int i=0; i<5; i++) { mylist.insert_front(i); mylist.insert_end(i+2); } //print mylist.display(); //拿到链表的front 结合起来 ListIter<ListNode<int>> begin(mylist.get_front()); //默认CTOR ListIter<ListNode<int>> end; //默认CTOR ListIter<ListNode<int>> iter; int findnum = 6; iter = find(begin,end,findnum); if(iter == end) cout<<findnum<<" not find"<<endl; else cout<<findnum<<" find"<<endl; return 0; }