[英]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: 所以作为一个类赋值我正在重新实现
std::vector
,而我在声明原型时遇到了麻烦:
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 我的vector类的模板看起来像这样
template<typename T>
class Vector
How can I declare the prototype for insert to return std::iterator
instead of my own VectorIterator
class? 如何声明insert的原型以返回
std::iterator
而不是我自己的VectorIterator
类? I will of course be returning an instance of my own VectorIterator class. 我当然会返回我自己的VectorIterator类的实例。
That function doesn't return a std::iterator
; 该函数不返回
std::iterator
; it returns a std::vector<T, Alloc>::iterator
. 它返回一个
std::vector<T, Alloc>::iterator
。 You need to typedef your VectorIterator
in your Vector
: 你需要在
Vector
你的VectorIterator
:
template <typename T>
class Vector {
typedef VectorIterator<T> iterator;
};
This is the return type of the insert
function. 这是
insert
函数的返回类型。 Any references to iterator
and const_iterator
in the std::vector
specification are to the typedefs that you need to provide. std::vector
规范中对iterator
和const_iterator
任何引用都是你需要提供的typedef。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.