假设我有这样的代码:
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`对象。
但是,如果我想将`std::shared_ptr`作为实现细节,那么经过一次解引用后,我应该得到一个`Something`对象。
也就是说:
Collection collection;
Collection::Iterator it = collection.begin();
Something firstMember = *it; // instead of the current **it;
我的问题是,我需要从头开始将迭代器作为
Collection
的嵌套类,并从这里http://www.cplusplus.com/reference/std/iterator/实现所有所需的随机访问迭代器函数吗?还是有一些众所周知的方法?可能是C++11?