初识STL——迭代器List

本文介绍了STL中的迭代器,作为其六大组件之一,迭代器提供了遍历容器元素的功能,类似于指针但提供了更丰富的操作,如*、++、==、!=和=。特别是针对list容器,讲解了list::iterator的模拟实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

迭代器

STL有6大组件

迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。
迭代器提供一些基本操作符:*、++、==、!=、=。这些操作和C/C++“操作array元素”时的指针接口一致。


  • list::iterator 的模拟实现

源代码

#pragma once

#include <iostream>
using namespace std;
#include <assert.h>

template<class T>
struct ListNode
{
    ListNode* _prev;
    ListNode* _next;
    T _data;
    ListNode(const T& x)
        :_data(NULL)
        , _prev(NULL)
        , _next(NULL)
    {}  
};

template<class T,class Ref,class Ptr>
struct ListIterator
{
    typedef ListNode<T> Node;
    typedef ListIterator<T, Ref, Ptr> Self;

    Node* _node;

    ListIterator(Node * node)
        :_node(node)
    {}
    bool operator==(const Self& x)
    {
        return _node == x._node;
    }
    bool operator!=(const Self& x)
    {
        return _node != x._node;
    }
    Ref operator*()
    {
        return _node->_data;
    }

    Ptr operator->()
    {
        return &(operatpr*());
    }

    Self& operator++() //前置++
    {
        _node = _node->_next;
        return *this;
    }
    Self operator++(int) //后置++
    {
        Self tmp(*this);
        _node = _node->_next;
        return tmp;
    }
    Self operator--(int)//后置--
    {
        Self tmp(*this);
        _node = _node->_prev;
        return tmp;
    }
    Self& operator--()//前置--
    {
        _node = _node->_prev;
        return *this;
    }
};

template<class T>
class List
{
public:
    typedef ListNode<T> Node;
    typedef ListIterator<T, T&, T*> Iterator;
    typedef ListIterator<const T,const T&,const T*> ConstIterator;
public:

    List()
        :_head(BuyNode(T()))
    {
        _head = _head->_next;
        _head = _head->_prev;
    }

    ~List()
    {
        Clear();
        delete _head;
        _head = NULL;
    }
    void Clear()
    {
        Node* cur = _head;
        while (cur != _head)
        {
            Node* next = cur->_next;
            delete cur;
            cur = next;
        }
        _head = _head->_next;
        _head = _head->_prev;
    }
    Iterator Begin()
    {
        return _head->_next;
    }
    ConstIterator Begin() const
    {
        return _head->_next;
    }
    Iterator End()
    {
        return _head;
    }
    ConstIterator End() const
    {
        return _head;
    }
    void PushBcak(const T& x)
    {
        Node* cur = BuyNode(x);
        Node* tail = _head->_prev;

        tail->_next = cur;
        cur->_prev = tail;
        cur->_next = _head;
        _head->_prev = cur;
    }

    void PushFront(const T& x)
    {
    /*  Node* cur = BuyNode(x);
        Node* next = _head->_next;
        if ()
        cur->_prev = _head;
        next->_next = cur;*/
    }

    Iterator Find(const T& x)
    {
        Node* cur = Begin();
        while (cur != _head)
        {
            if (*cur == x)
            {
                return cur;
            }
            ++cur;
        }
        return _head;
    }
    Iterator Erase(Iterator pos)
    {
        assert(pos._node&&pos._node != _head);
        Node* Next = pos._node->_next;
        Node* Prev = pos._node->_prev;

        Next->_prev = Prev;
        Prev->_next = Next;
        delete pos._node;
        return Next;
    }

    Iterator Insert(Iterator pos, const T& x)
    {
        assert(pos._node);
        Node* Next = pos._node;
        Node* Prev = Next->_prev;

        Node* Tmp = new Node(x);

        Next->_prev = Tmp;
        Tmp->_next = Next;
        Prev->_next = Tmp;
        Tmp->_prev = Prev;

        return Tmp;
    }
    void Print()
    {
        Node* cur = _head;
        while (cur->_next != _head)
        {
            cur = cur->_next;
            cout << cur->_data << "  ";
        }
        cout << endl;
    }
protected:
    Node* BuyNode(const T& x)
    {
        return new Node(x);
    }
protected:
    Node* _head;
};


void TestList()
{
    List<int> l1;
    l1.PushBcak(1);
    l1.Print();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值