NoFear
随笔 - 48, 文章 - 0, 评论 - 6, 阅读 - 58938

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

简单实现一个迭代器

Posted on   Fear_Hao  阅读(509)  评论(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; }
复制代码

 

 

 

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示