Q:

How to define a iterator in my class

I use iterator in C++.It is so good.But I want have a my iterator in my class.how to do?
a simple example
//a simple container
class MyArray
{
public:
    MyArray()
    {
        for(int i=0;i<10;i++)
            data[i] = 0;
    }

    int GetValue(int index)
    {   
        if(index>=10 || index<0)
            return -1;
        else
            return data[index]; 
    }

    bool SetValue(int value, int index)
    {
        if(index>=10 || index<0)
            return false;
        data[index] = value;
    }

    int& operator[](int index)
    {   return data[index]; }

    void ShowData();
protected:
    int data[10];
private:
};

// only test use
void MyArray::ShowData()
{
    std::cout<<"Data : ";
    for(int i =0;i<10;i++)
    {
        std::cout<<data[i]<<" ";
    }
    std::cout<<std::endl;
}

int main()
{
    MyArray array;

    MyArray::iterator it = array.begin();   //how to implementation?

    getchar();
    return 0;
}

A:

The C++11 standard defines the requirements for iterators in section 24 of the standard. So, the short answer here is for you to define and implement your iterator and const_iterator class in a manner that meets the requirements for iterators.
The requirements for iterators take up five terse pages in the standard. Obviously, that's not something that can be summarized in a few short paragraphs in a stackoverflow.com answer.
I would suggest that you visit a library or a book store, look for a thorough book on C++, browse it, and see if it provides a good, substantive review of iterators and their requirements. Then try to implement them yourself.

