马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 andalousie 于 2014-3-7 20:30 编辑
声明一下,这不是在实现<vector>库,而是基于C++的vector给出一个iterator方法。vc6.0/g++编译通过。第一个版本:#include <iostream>
#include <vector>
using namespace std;
template <class Object>
class VectorIterator;
// Same as the vector, but has a getIterator method.
// No extra data, no overridden methods, so non-virtual
// destructor in original vector is OK!
template <class Object>
class MyVector : public vector<Object>
{
public:
explicit MyVector( int size = 0 )
: vector<Object>( size ) { }
VectorIterator<Object> getIterator( ) const
{ return VectorIterator<Object>( this ); }
};
// A passive iterator class. Steps through its MyVector.
template <class Object>
class VectorIterator
{
public:
VectorIterator( const MyVector<Object> *v )
: owner( v ), count( 0 ) { }
bool hasNext( ) const
{ return count != owner->size( ); }
const Object & next( )
{ return (*owner)[ count++ ]; }
private:
const MyVector<Object> *owner;
int count;
};
int main( )
{
MyVector<int> v;
v.push_back( 3 );
v.push_back( 2 );
cout << "Vector contents: " << endl;
VectorIterator<int> itr = v.getIterator( );
while( itr.hasNext( ) )
cout << itr.next( ) << endl;
return 0;
}
注意,这里我们的itr不再表现为指针的样子,类似Java的方法。
第二个版本:
class MyVector内容不做改变,增加VectorIterator的方法。...
// A passive iterator class. Steps through its MyVector.
template <class Object>
class VectorIterator
{
public:
VectorIterator( const MyVector<Object> *v )
: owner( v ), count( 0 ) { }
void reset( )
{ count = 0; }
bool isValid( ) const
{ return count < owner->size( ); }
void advance( )
{ count++; }
const Object & retrieve( ) const
{ return (*owner)[ count ]; }
private:
const MyVector<Object> *owner;
int count;
};
int main( )
{
MyVector<int> v;
v.push_back( 3 );
v.push_back( 2 );
VectorIterator<int> itr = v.getIterator( );
for( int i = 0; i < 2; i++ )
{
cout << "Vector contents: " << endl;
for( itr.reset( ); itr.isValid( ); itr.advance( ) )
cout << itr.retrieve( ) << endl;
}
return 0;
}
第三个版本:基于继承的迭代器...
template <class Object>
class Iterator;
template <class Object>
class VectorIterator;
// Same as the vector, but has a getIterator method.
// No extra data, no overridden methods, so non-virtual
// destructor in original vector is OK!
template <class Object>
class MyVector : public vector<Object>
{
public:
explicit MyVector( int size = 0 )
: vector<Object>( size ) { }
Iterator<Object> *getIterator( ) const
{ return new VectorIterator<Object>( this ); }
};
// A passive iterator class protocol. Steps through its container.
template <class Object>
class Iterator
{
public:
virtual ~Iterator( ) { }
virtual bool hasNext( ) const = 0;
virtual const Object & next( ) = 0;
};
// A concrete implementation of the iterator.
// Could have been nested inside of MyVector!
template <class Object>
class VectorIterator : public Iterator<Object>
{
public:
VectorIterator( const MyVector<Object> *v )
: owner( v ), count( 0 ) { }
bool hasNext( ) const
{ return count != owner->size( ); }
const Object & next( )
{ return (*owner)[ count++ ]; }
private:
const MyVector<Object> *owner;
int count;
};
int main( )
{
MyVector<int> v;
v.push_back( 3 );
v.push_back( 2 );
cout << "Vector contents: " << endl;
Iterator<int> *itr = v.getIterator( );
while( itr->hasNext( ) )
cout << itr->next( ) << endl;
delete itr;
return 0;
}
缺点:由于*itr是动态分配的,所以需要delete。
大家如果不放心,用auto_ptr也可以。
就是36行改成 auto_ptr<Iterator<Object> > getIterator( ) const
{ return auto_ptr<Iterator<Object> >(new VectorIterator<Object>( this )); }
82行改成 auto_ptr<Iterator<int> > itr = v.getIterator( );
就可以了。
|