如何使迭代器"自动解除引用"?

Mar*_*dik 4 c++ iterator c++11

假设我有这样的事情:

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()}
}
Run Code Online (Sandbox Code Playgroud)

当我使用一个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;
Run Code Online (Sandbox Code Playgroud)

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

Ant*_*lov 10

这听起来像你正在实现的东西存在并被称为boost::ptr_vector.Boost还提供了一个用于实现迭代器的库,减少了痛苦.这听起来像你正在寻找的是boost::indirect_iterator