zoukankan      html  css  js  c++  java
  • 迭代器模式

    一、今日学习内容

    1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从小到大和从大到小两种次序输出学生信息。

    类图:

     

    测试结果:

      

     c++:

    #include <iostream>  
    #include<string>
    #include <sstream>
    #include <vector>  
    using namespace std;  
     
    /* object可以是任意类型的变量 */  
    typedef int object;  
     
    class Iterator  
    {  
    public:  
        virtual object begin() = 0;  
        virtual void   next() = 0;  
        virtual object end() = 0;  
        virtual object current() = 0;  
        virtual bool   IsDone() = 0;  
    };  
     
    class ConcreteAggregate  
    {  
    private:  
        vector<object> _objects;  
     
    public:  
        void AddObject(object obj)  
        {  
            _objects.push_back(obj);  
        }  
     
        object& operator[](int index)  
        {  
            return _objects[index];  
        }  
        int size()  
        {  
            return _objects.size();  
        }  
    };  
     
    class ConcreteIterator:public Iterator  
    {  
    public:  
        ConcreteAggregate *agg;   
        int _index;  
    public:  
        ConcreteIterator(ConcreteAggregate *agg)  
        {  
            this->agg = agg;  
            _index = 0;  
        }  
        virtual object begin()  
        {  
            return (*agg)[0];  
        }  
        virtual void next()  
        {  
            _index++;  
        }  
        virtual void preious()  
        {  
            _index--;  
        }   
     
        virtual object end()  
        {  
            _index = agg->size();  
            return (*agg)[_index-1];  
        }  
     
        virtual object current()  
        {  
            return (*agg)[_index];  
        }  
     
        virtual bool IsDone()  
        {  
            return (_index == agg->size());  
        }  
        virtual bool IsFirst()  
        {  
            return (_index == 0);  
        }  
    };  
     
    int main()  
    {  
        ConcreteAggregate *objects =new ConcreteAggregate();  
        cout<<"信1305班同学:"<<endl;
        for(int i=1;i<=44;i++)
        {
            int m=20130000+i;
            objects->AddObject(m); 
            m=0;
        }
     
        ConcreteIterator *iter = new ConcreteIterator(objects);  
        ConcreteIterator *iter1 = new ConcreteIterator(objects);  
     
        object tmp_begin = iter->begin();  
        cout<<"从小到大输出:"<<endl;
        while(!iter->IsDone())  
        {  
            cout<<iter->current()<<endl;  
            iter->next();  
        }  
        cout<<endl;  
    
        object tmp_begin1 = iter1->end();  
        cout<<"从大到小输出:"<<endl;
        while(!iter1->IsFirst())  
        {  
            iter1->preious();
            cout<<iter1->current()<<endl;    
        }  
        cout<<endl; 
     
        delete objects;  
        delete iter;  
     
        system("pause");  
        return 0;  
    } 
  • 相关阅读:
    js 兼容各类手机 的写法 待续
    css 兼容 各类手机的写法 待续
    数组的解构赋值
    let 和 const 命令
    ECMAScript 6 简介
    webpack4新建一个项目
    Webpack 4 Tutorial: from 0 Conf to Production Mode
    webpack4.1.1的使用详细教程
    git merge git pull时候遇到冲突解决办法git stash
    Git 常用命令速查表(图文+表格)
  • 原文地址:https://www.cnblogs.com/wmdww/p/15574566.html
Copyright © 2011-2022 走看看