开发者

How to declare function prototype that returns std iterator?

So as a class assignment I'm reimplementing std::vector, and I'm having trouble with declaring the prototype for:

iterator insert ( iterator position, const T& x );

The template for my iterator class looks like this

template<typename T>
class VectorIterator : public std::iterator开发者_如何学Python<std::input_iterator_tag, T>

The template for my vector class looks like this

template<typename T>
class Vector 

How can I declare the prototype for insert to return std::iterator instead of my own VectorIterator class? I will of course be returning an instance of my own VectorIterator class.


That function doesn't return a std::iterator; it returns a std::vector<T, Alloc>::iterator. You need to typedef your VectorIterator in your Vector:

template <typename T>
class Vector {
    typedef VectorIterator<T> iterator;
};

This is the return type of the insert function. Any references to iterator and const_iterator in the std::vector specification are to the typedefs that you need to provide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜