自定义迭代器 [英] custom iterator

查看:65
本文介绍了自定义迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在实现一个自定义迭代器(随机访问类型)所以我可以使用

stl算法,比如对我的遗产进行排序容器。然而,我正在编译
问题。当我实现我的customIterator类时,我已经定义了下面的运算符(以及其他人,但他们现在不是很重要的,我不认为)。


class customIterator

{


customerIterator& operator *()const;

int operator-(const customIterator& x);

int operator-(const contIter& rhs)const

customIterator&安培; operator ++();

customIterator operator ++(int);

customIterator& operator - ();

customIterator operator - (int);

customIterator& operator + =(difference_type n);

customIterator& operator - =(difference_type n);

customIterator operator +(difference_type rhs)const;

customIterator operator-(difference_type rhs)const;

int& ; operator [](int n);

bool operator ==(const customIterator& x)const;

bool operator<(const customIterator& x)const;


}

问题是当我将其中一个迭代器传递给

std :: sort时,我很想念所有各种运算符,如运算符>(int),

运算符/(int),运算符 - (int)。


这些是生成的,因为stl: :在我的平台上排序有代码

像这样;


while(__last - __first> __stl_threshold){


或使用类似的表达式;


如果((__last - __first)/ 2>某事......)


我需要吗?提供一个操作符,用于将我的

customIterator隐式转换为int,以便可以评估比较(__last - first>

__stl_threshold)?


现在我只是想用一个简单的案例来编译这个元素

向量的向量。

std ::矢量<诠释> vint;


containerWrapper包装器(vint);

customIterator begin = wrapper.begin();

customIterator end = wrapper。结束();

//这里的编译错误.......

std :: sort(开始,结束,aCompareFunction());


编译错误就像;


1)运算符>没有在参数类型customIterator中实现

类型为int


2)operator ==未在参数类型customIterator中实现

类型int


3)运算符/未在类型customIterator中实现参数

类型为int

等。等等


任何想法?


谢谢。

Hi,

I''m implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I''m having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they''re not
important right now, I don''t think).

class customIterator
{

customerIterator& operator*() const;
int operator-(const customIterator& x);
int operator-(const contIter& rhs) const
customIterator& operator++();
customIterator operator++(int);
customIterator& operator--();
customIterator operator--(int);
customIterator& operator+=(difference_type n);
customIterator& operator-=(difference_type n);
customIterator operator+(difference_type rhs) const;
customIterator operator-(difference_type rhs) const;
int& operator[](int n);
bool operator==(const customIterator& x) const;
bool operator<(const customIterator& x) const;

}
The problem is when I come to pass one of these iterators into
std::sort, I''m missing all sorts of operators like operator>(int),
operator/(int), operator-(int).

These are being generated because the stl::sort on my platform has code
like this;

while (__last - __first > __stl_threshold) {

or uses expressions like;

if ( (__last - __first)/2 > something... )

Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?

Right now I''m just trying to get this to compile with a simple case
vectors of ints.
std::vector<int> vint;

containerWrapper wrapper(vint);
customIterator begin = wrapper.begin();
customIterator end = wrapper.end();
// compilation errors here .......
std::sort(begin, end, aCompareFunction());

compilation errors are like;

1) operator> not implemented in the type customIterator for arguments
of type int

2) operator== not implemented in the type customIterator for arguments
of type int

3) operator/ not implemented in the type customIterator for arguments
of type int
etc. etc.

any ideas?

thanks.

推荐答案

"Gr*****@nospam.com" <的Gr ********** @ gmail.com> schrieb im Newsbeitrag

新闻:11 ********************* @ g49g2000cwa.googlegro ups.com ...

[...]
"Gr*****@nospam.com" <Gr**********@gmail.com> schrieb im Newsbeitrag
news:11*********************@g49g2000cwa.googlegro ups.com...
[...]
class customIterator
{
[...]}
[...] //这里的编译错误... ....
std :: sort(begin,end,aCompareFunction());
编译错误就像;
1)运算符>未在类型为customIterator的类型中实现
类型为int
2)operator ==未在类型为customIterator的类型中实现
类型为int
3)operator / not implemented in参数的类型customIterator
类型为int
class customIterator
{ [...] } [...] // compilation errors here .......
std::sort(begin, end, aCompareFunction());
compilation errors are like;
1) operator> not implemented in the type customIterator for arguments
of type int
2) operator== not implemented in the type customIterator for arguments
of type int
3) operator/ not implemented in the type customIterator for arguments
of type int




您可以在全球定义错过的运算符,即


bool运算符>(const customIterator& i1,const customIterator& i2){return

DistanceOf(i1,i2)> 0; }

bool operator ==(const customIterator& i1,const customIterator& i2){return

DistanceOf(i1,i2)== 0; }

....

int DistanceOf(const customIterator& i1,const customIterator& i2)

{

//计算迭代器的距离,这可能取决于迭代器内部的
定义

}


希望这有帮助。有问题吗?


// oliver



you can define the missed operators globally, ie

bool operator>(const customIterator&i1, const customIterator&i2) { return
DistanceOf(i1, i2) > 0; }
bool operator==(const customIterator&i1, const customIterator&i2) { return
DistanceOf(i1, i2) == 0; }
....
int DistanceOf(const customIterator&i1, const customIterator&i2)
{
// calculate the distance of your iterators, which may depend on the inner
definition of the iterator
}

Hope this helps. Any questions?

// oliver


* Gr ***** @ nospam.com
* Gr*****@nospam.com:
我正在实现一个自定义迭代器(随机访问类型)所以我可以使用
stl算法,例如对我的旧容器进行排序。然而,我正在编译问题。在实现我的customIterator类时,我已经定义了下面的运算符(以及其他运算符,但它们现在不重要,我不认为)。

class customIterator
{/ /
customerIterator& operator *()const;
int operator-(const customIterator& x);
int operator-(const contIter& rhs)const
customIterator& operator ++();
customIterator operator ++(int);
customIterator& operator - ();
customIterator operator - (int);
customIterator& operator + =(difference_type n);
customIterator& operator - =(difference_type n);
customIterator operator +(difference_type rhs)const;
customIterator operator-(difference_type rhs)const;
int& operator [](int n);
bool operator ==(const customIterator& x)const;
bool operator<(const customIterator& x)const;

}

问题是当我将其中一个迭代器传递给
std :: sort时,我错过了各种类型的运算符,如operator>(int),
operator / (int),operator-(int)。

这些是生成的,因为我平台上的stl :: sort有这样的代码

while( __last - __first> __stl_threshold){

或使用类似的表达式;

如果((__last - __first)/ 2>某事......)


对随机访问迭代器的要求大致是它像指针一样表现为




包括比较和减法但是不是除法。


我是否需要提供一个运算符来将我的
customIterator隐式转换为int以进行比较(__last - first>
__stl_threshold)可以是eval uated?


不,你需要定义


std :: ptr_diff operator-(MyIter const& a,MyIter const& b)


现在我只是想通过一个简单的案例
向量的向量来编译这个。

std :: vector< int> vint;

containerWrapper包装器(vint);
customIterator begin = wrapper.begin();
customIterator end = wrapper.end();

//编译错误这里.......
std :: sort(开始,结束,aCompareFunction());

编译错误就像;
1)运算符>未在类型为customIterator的类型中实现
类型为int

2)operator ==未在类型为customIterator的类型中实现
类型为int
3)运算符/未在类型为customIterator中实现的参数类型为int
I''m implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I''m having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they''re not
important right now, I don''t think).

class customIterator
{

customerIterator& operator*() const;
int operator-(const customIterator& x);
int operator-(const contIter& rhs) const
customIterator& operator++();
customIterator operator++(int);
customIterator& operator--();
customIterator operator--(int);
customIterator& operator+=(difference_type n);
customIterator& operator-=(difference_type n);
customIterator operator+(difference_type rhs) const;
customIterator operator-(difference_type rhs) const;
int& operator[](int n);
bool operator==(const customIterator& x) const;
bool operator<(const customIterator& x) const;

}
The problem is when I come to pass one of these iterators into
std::sort, I''m missing all sorts of operators like operator>(int),
operator/(int), operator-(int).

These are being generated because the stl::sort on my platform has code
like this;

while (__last - __first > __stl_threshold) {

or uses expressions like;

if ( (__last - __first)/2 > something... )
The requirements on a random access iterator are, roughly, that it behaves
like a pointer.

That includes comparision and subtraction, but not division.

Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?
No, you need to define

std::ptr_diff operator-( MyIter const& a, MyIter const& b )

Right now I''m just trying to get this to compile with a simple case
vectors of ints.
std::vector<int> vint;

containerWrapper wrapper(vint);
customIterator begin = wrapper.begin();
customIterator end = wrapper.end();
// compilation errors here .......
std::sort(begin, end, aCompareFunction());

compilation errors are like;

1) operator> not implemented in the type customIterator for arguments
of type int

2) operator== not implemented in the type customIterator for arguments
of type int

3) operator/ not implemented in the type customIterator for arguments
of type int




我更怀疑你的观点(3)。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



I rather doubt your point (3).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




" Alf P. Steinbach" <人*** @ start.no>在消息中写道

news:43 **************** @ news.individual.net ...

"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* Gr ***** @ nospam.com
* Gr*****@nospam.com:
我正在实施一个自定义迭代器(随机访问类型)所以我可以使用
stl算法,例如对我的旧容器进行排序。然而,我正在编译问题。在实现我的customIterator类时,我已经定义了下面的运算符(以及其他运算符,但它们现在不重要,我不认为)。
I''m implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I''m having
problems compiling however. when implementing my customIterator class I
have defined the operators below (as well as others but they''re not
important right now, I don''t think).



[snip]


[snip]

我是否需要提供一个运算符来将我的
customIterator隐式转换为int以进行比较(__last - first>
__stl_threshold )可以评估?
Do I need to provide an operator for implicit conversion of my
customIterator to an int so that the comparison ( __last - first >
__stl_threshold) can be evaluated?



不,你需要定义

std :: ptr_diff operator-(MyIter const& a,MyIter const& b)



No, you need to define

std::ptr_diff operator-( MyIter const& a, MyIter const& b )




我读过这个吗?在命名空间''std''中定义一些东西?

标准是否允许这样做?


-Mike



Did I read that right? Define something in namespace ''std''?
Is that allowed by the standard?

-Mike

这篇关于自定义迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆