鱼C论坛

 找回密码
 立即注册
查看: 2157|回复: 0

[技术交流] 浅析iterator

[复制链接]
发表于 2014-3-7 20:28:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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( );
就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-23 18:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表