• 设计模式:迭代器模式(C++实现)


    迭代器模式是一种行为型设计模式,用于提供一种遍历集合对象的统一接口。在C++中,可以使用以下步骤实现迭代器模式:

    #include 
    #include 
    
    // 抽象迭代器接口
    class Iterator
    {
    public:
        virtual bool hasNext() = 0;
        virtual int next() = 0;
    };
    
    // 集合类接口
    class Aggregate
    {
    public:
        virtual Iterator *createIterator() = 0;
        virtual void addItem(int item) = 0;
        virtual int getItem(int index) = 0; // 添加getItem()方法
        virtual int getSize() = 0;          // 添加getSize()方法
    };
    
    // 具体迭代器类
    class ConcreteIterator : public Iterator
    {
    private:
        Aggregate *aggregate;
        int position;
    
    public:
        ConcreteIterator(Aggregate *a) : aggregate(a), position(0) {}
    
        bool hasNext() override
        {
            return position < aggregate->getSize();
        }
    
        int next() override
        {
            int item = aggregate->getItem(position);
            position++;
            return item;
        }
    };
    
    // 具体集合类
    class ConcreteAggregate : public Aggregate
    {
    private:
        std::vector<int> items;
    
    public:
        Iterator *createIterator() override
        {
            return new ConcreteIterator(this);
        }
    
        void addItem(int item) override
        {
            items.push_back(item);
        }
    
        int getItem(int index) override
        { 
            // 实现getItem()方法
            return items[index];
        }
    
        int getSize() override
        { 
            // 实现getSize()方法
            return items.size();
        }
    };
    
    int main()
    {
        ConcreteAggregate aggregate;
        aggregate.addItem(1);
        aggregate.addItem(3);
        aggregate.addItem(2);
    
        Iterator *iterator = aggregate.createIterator();
        while (iterator->hasNext())
        {
            int item = iterator->next();
            // 处理每个元素
            std::cout << item << "\n";
        }
    
        delete iterator;
    
        return 0;
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    运行结果:

    1
    3
    2
    
    • 1
    • 2
    • 3
  • 相关阅读:
    【LeetCode热题100】--15.三数之和
    认识微服务 SpringCloud (史上最全学习路线)
    【学习笔记】AGC018
    【仿真建模-anylogic】动态生成ConveyorCustomStation
    thinkphp5 URL和路由的功能详解与实例
    从「博客园」的困境,到「用爱发电」~
    低代码之JeecgBoot
    码蹄集 - MT3521 - X/Y
    minikube 快速使用入门
    大型能源装备制造企业数字化转型实践----工业软件讲坛第八次讲座
  • 原文地址:https://blog.csdn.net/wydxry/article/details/133234087