4

假设我有这样的事情:

class Collection
{
private:
    typedef std::vector< std::shared_ptr<Something> >::iterator Iterator;
    std::vector< std::shared_ptr<Something> > data_;

public:
    Iterator begin() {return data_.begin()}
    Iterator end()  {return data_.end()}
}

当我使用Collection::Iterator实例时,我需要取消引用它一次,以获取std::shared_ptr<Something>对象并再次获取Something对象。

但是如果我只想做std::shared_ptr<Something>一个实现细节,那么在一次取消引用之后,我应该得到一个Something对象是合理的。

那是:

Collection collection;
Collection::Iterator it = collection.begin();
Something firstMember = *it;  // instead of the current **it;

我的问题是,我是否需要Collection从头开始将 Iterator 作为嵌套类,并从这里http://www.cplusplus.com/reference/std/iterator/实现随机访问迭代器所需的所有功能,或者在那里一些众所周知的方法?可能是 C++11?

4

1 回答 1

10

听起来你正在实施的东西存在并且被称为boost::ptr_vector. Boost 还提供了一个库,用于实现更轻松的迭代器。听起来你正在寻找的是boost::indirect_iterator

于 2012-07-09T04:20:03.473 回答