简体   繁体   中英

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<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM