从模板类返回迭代器 [英] Returning iterator from template class
问题描述
大家好,
我正在尝试为std :: vector编写一个包装类,以扩展其功能的一部分。我遇到的问题是从成员函数返回一个
迭代器类型。这是我想要做的事情的本质:
// -------代码------ -
#include< vector>
using namespace std;
template< class T> < br $>
class ExtendedVector
{
private:
vector< T> vals;
public:
vector< T> :: iterator begin()
{
返回vals。开始();
}
};
// -------结束代码---- ---
然而,这给了我错误
错误:预期'';''在''开始'之前''br />
实现我想做的事情的正确方法是什么?
Hi All,
I''m trying to write a wrapper class for std::vector to extend some of
its functionality. The problem I''m getting into is returning an
iterator type from a member function. Here is the essense of what I''m
trying to do:
//-------code-------
#include <vector>
using namespace std;
template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public:
vector<T>::iterator begin()
{
return vals.begin();
}
};
//-------end code-------
However, this gives me the error
error: expected '';'' before ''begin''
What''s the proper way to achieve what I''m trying to do?
推荐答案
jois.de.vi ... @ gmail.com写道:
这样实施:
jois.de.vi...@gmail.com wrote:
Implement thus:
模板<类T>
类ExtendedVector
{
私有:
vector< T> vals;
public:
//以下不仅可以节省打字,还可以让你实现
//使用不同的对象类型矢量...只需
在这里更改defs
//只要他们不尝试,客户就会排队
绕过你的界面。
typedef typename vector< T> :: iterator iterator_t;
typedef typename vector< T> :: const_iterator const_iterator_t;
iterator_t begin()//将返回类型更改为typedef以上
{
返回vals.begin();
}
//实现const对象.. 。
const_iterator_t begin()const {return vals.begin(); // ---------结束代码-------
template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public: // the following not only saves typing but allows you to
implement
// with a different object type other than vector...just
change the defs here
// and clients will fall in line so long as they don''t try
to bypass your interface.
typedef typename vector<T>::iterator iterator_t;
typedef typename vector<T>::const_iterator const_iterator_t;
iterator_t begin() // changed return type to above typedefs
{
return vals.begin();
} // implement for const objects...
const_iterator_t begin() const { return vals.begin(); } };
//-------end code-------
ExtendedVector< T> t;
ExtendedVector< T> :: iterator_t it;
ExtendedVector<T> t;
ExtendedVector<T>::iterator_t it;
< jo * **********@gmail.com>在消息中写道
news:11 ********************* @ g10g2000cwb.googlegro ups.com ...
<jo***********@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
大家好,
我正在尝试为std :: vector编写一个包装类来扩展它的一些功能。我遇到的问题是从成员函数返回一个
迭代器类型。这是我想要做的事情的本质:
// -------代码-------
> #include< vector>
使用命名空间std;
模板< class T>
类ExtendedVector
{
私人:
矢量< T> vals;
public:
vector< T> :: iterator begin()
Hi All,
I''m trying to write a wrapper class for std::vector to extend some of
its functionality. The problem I''m getting into is returning an
iterator type from a member function. Here is the essense of what I''m
trying to do:
//-------code-------
#include <vector>
using namespace std;
template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public:
vector<T>::iterator begin()
typename vector< T> :: iterator begin()
问候,
Sumit。
-
Sumit Rajan< su **** *****@gmail.com>
typename vector<T>::iterator begin()
Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
>
typedef typename vector< T> :: iterator iterator_t;
typedef typename vector< T> :: const_iterator const_iterator_t;
typedef typename vector<T>::iterator iterator_t;
typedef typename vector<T>::const_iterator const_iterator_t;
iterator_t begin()//将返回类型更改为typedef
{
返回vals.begin() ;
}
iterator_t begin() // changed return type to above typedefs
{
return vals.begin();
}
//为const对象实现...
const_iterator_t begin()const {return vals.begin(); }
// implement for const objects...
const_iterator_t begin() const { return vals.begin(); }
很棒!我想我的下一个问题是,你怎么知道这样做的?
这一点我甚至不知道上面代码片段中
的typedef typename部分是什么意思。更具体地说,我想是,我可以在哪里获得有关模板的详细讨论。关于
书籍的任何建议?网站?
Wonderful! I guess my next question is, how did you know to do that? At
this point I don''t even know what the ''typedef typename'' part of the
above code snippet means. More specifically I suppose is, where can I
get a detailed discussion about templates. Any recommendations on
books? web sites?
这篇关于从模板类返回迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!