迭代器模式(C++)

#include 
#include <string>
#define CAP 100
using namespace std;

class group
{
public:
    group():pc(0){}
    virtual ~group(){}
    friend class Iterator;
    void push(string m)
    {
        if (pc<CAP)
            vec[pc++]=m;
    }
    string pop()
    {
        if (!isempty())
            return vec[--pc];
    }
    bool isempty()
    {
        if (pc<=0)
        {
            return true;
        }
        return false;
    }

private:
    int pc;
    string vec[CAP];
};

class absIterator
{
public:
    absIterator(){}
    virtual ~absIterator(){}
    virtual string operator++()=0;
    virtual string operator++(int)=0;
};

class Iterator : public absIterator
{
public:
    Iterator(group *gup):index(0),p_gup(gup){}
    virtual ~Iterator(){}
    string operator++()
    {
        return p_gup->vec[++index];
    }

    string operator++(int)
    {
        return p_gup->vec[index++];
    }

private:
    group* p_gup;
    int index;
};

int main()
{
    group *gp=new group;
    gp->push("sadfs");
    gp->push("werwer");
    gp->push("28934759");

    Iterator* it=new Iterator(gp);
    for (int i=0;i<3;i++)
    {
        cout<<(*it)++<<endl;
    }
    delete it;
    delete gp;

    system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/tiandsp/archive/2012/06/28/2567402.html

你可能感兴趣的:(迭代器模式(C++))