// @topic S-0314-11-02-10 C++ iterator design pattern II
// @brief C++ Iterator class demo

#include <iostream>
#include <algorithm>
void pause()
{
    std::cout << "Enter x to exit: ";
    char ch;
    std::cin >> ch;
}

class Container; // forward declaration
class Iterator {
    Container* container;
    int position;
public:
    Iterator( Container* container, int position )
        : container( container ), position( position )
    {
    }
    bool equals( Iterator another )
    {
        return ( position == another.position );
    }

    char get_value();

    void next()
    {
        ++position;
    }
};//class Iterator

class Container {
    static const int MAX_CONTAINER_SIZE = 100;
    char sequence[ MAX_CONTAINER_SIZE ];
    int length;
public:
    typedef Iterator iterator; // now Container::Iterator is a synonym to Iterator

    Container( char* ptr )
    {
        length = strlen( ptr );
        std::copy( ptr, ptr + length, sequence );
    }
    char at( int position )
    {
        return sequence[ position ];
    }

    Iterator begin()
    {
        return Iterator( this, 0 );
    }

    Iterator end()
    {
        return Iterator( this, size() );
    }

    int size()
    {
        return length;
    }
};//class Container

char Iterator::get_value()
{
    return container->at( position );
}

int main()
{
    Container collection( "Hello" );
    //Iterator iterator = collection.begin();
    Container::iterator iterator = collection.begin();
    while ( !iterator.equals( collection.end() ) ) {
        std::cout << iterator.get_value(); // *iterator
        iterator.next();                   // ++iterator
    }
    std::cout << '\n';
    pause();
    return 0;
}