- #include "stdafx.h"
- namespace MyClass
- {
- #define MAXCOUNT 255
- template<class T>
- class MyvectorInt
- {
- public:
- class Iterator
- {
- public:
- Iterator( MyvectorInt<T> &vector, int iIndex ):m_vecotr( vector ), m_iIndex(iIndex){}
- Iterator& operator++( )
- {
- m_iIndex ++;
- return *this;
- }
- Iterator& operator=(const Iterator& other )
- {
- m_iIndex = other.m_iIndex;
- return *this;
- }
- bool operator!=( const Iterator& other )
- {
- return m_iIndex!=other.m_iIndex;
- }
- T* operator->() const
- {
- return (T*)(this->m_vecotr.m_Data + m_iIndex);
- }
- T& operator*() const
- {
- return this->m_vecotr.m_Data[m_iIndex];
- }
- private:
- int m_iIndex;
- MyvectorInt<T> &m_vecotr;
- };
- private:
- T m_Data[MAXCOUNT];
- int m_iSize;
- public:
- MyvectorInt<T>()
- {
- m_iSize = 0;
- }
- Iterator begin()
- {
- return Iterator( *this, 0 );
- }
- Iterator end()
- {
- return Iterator( *this, m_iSize );
- }
- int size()
- {
- return m_iSize;
- }
- Iterator insert( T &x )
- {
- if( m_iSize == MAXCOUNT )
- {
- return Iterator( *this, m_iSize );
- }
- memcpy( m_Data+m_iSize, &x, sizeof(T) );
- m_iSize ++;
- return Iterator( *this, m_iSize -1 );
- }
- };
- }
- typedef struct _x
- {
- int a;
- int b;
- } X;
- int _tmain(int argc, _TCHAR* argv[])
- {
- MyClass::MyvectorInt<X> vector;
- X x;
- for( int i = 0; i < 10; i ++ )
- {
- x.a = 1;
- x.b = 2;
- vector.insert( x );
- }
- for( MyClass::MyvectorInt<X>::Iterator it = vector.begin(); it != vector.end(); it ++ )
- {
- printf( "%d/r/n", it->b );
- }
- return 0;
- }