迭代器问题...... [英] Iterator questions...

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

问题描述

我注意到当容器为空时,container.begin()和container.end()都没有返回

错误,所以当我的时候我得到一个讨厌的段错误

取消引用迭代器。每次使用迭代器时,我是否必须检查容器是否为
为空?或者我错过了什么?


说到迭代器:如果我初始化''iter = container.end()'',我可以

指望iter保持等于container.end()即使元素被删除并添加到
容器?

I''ve noticed that neither container.begin() nor container.end() return
an error when the container is empty, so I get a nasty segfault when I
dereference the iterator. Do I have to check if the container is
empty every time I use an iterator, or am I missing something?

Speaking of iterators: if I initialize ''iter = container.end()'', can I
count on iter to stay equal to container.end() even if elements are
deleted and added to the container?

推荐答案

barcaroller写道:
barcaroller wrote:

我注意到了container.begin()和container.end()返回

容器为空时出错,
I''ve noticed that neither container.begin() nor container.end() return
an error when the container is empty,



他们为什么要这样做?空范围是完全有效的,std :: sort()会很高兴排序空范围。

Why should they? Empty ranges are perfectly valid and std::sort() will
happily sort an empty range.


所以我得到一个令人讨厌的段错误我取消引用迭代器。
so I get a nasty segfault when I dereference the iterator.



解除引用无效迭代器是未定义的行为。你很幸运

你的平台做了正确的事情并提醒你这个bug。

Dereferencing an invalid iterator is undefined behavior. You got lucky that
your platform did the right thing and alerted you of the bug.


我是否必须检查容器是否是
每次使用迭代器时
为空,
Do I have to check if the container is
empty every time I use an iterator,



在取消引用之前,必须确保迭代器有效。如果

你不知道容器是否是空的,你将不得不检查。

You have to ensure that the iterator is valid before you dereference it. If
you don''t know whether the container is empty, you will have to check.


或我错过了什么?
or am I missing something?



不,你似乎是现货。

No, you seem to be spot on.


说到迭代器:if我初始化''iter = container.end()'',我可以

指望iter保持等于container.end(),即使元素是

删除了添加到容器?
Speaking of iterators: if I initialize ''iter = container.end()'', can I
count on iter to stay equal to container.end() even if elements are
deleted and added to the container?



这取决于容器:如果我没记错的话,只有std :: list和

关联容器才能做出这种保证。

最好


Kai-Uwe Bux

That depends on the container: if I recall correctly, only std::list and the
associative containers make that kind of guarantee.
Best

Kai-Uwe Bux


barcaroller写道:
barcaroller wrote:

我注意到容器为空时,container.begin()和container.end()都没有返回

错误,所以我得到一个令人讨厌的段错误当我将
取消引用迭代器时。每次使用迭代器时,我是否必须检查容器是否为
为空?或者我错过了什么?
I''ve noticed that neither container.begin() nor container.end() return
an error when the container is empty, so I get a nasty segfault when I
dereference the iterator. Do I have to check if the container is
empty every time I use an iterator, or am I missing something?



你错过了什么。对于一个空的容器.begin()==。end()


你不能检查你的iteratores是否等于.end()吗? />

.begin()和.end()最常见的用途之一是for循环,例如

as:


std :: list< intData;

for(std :: list< int> :: iterator it = Data.begin(); it!= Data.end(); + +它)

{

//保证指向列表中的项目

}

其他用途包括将它们传递给算法,也是因为.find()

等...


但是在所有情况下你必须确保你的迭代器!= .end()

You are missing something. For an empty containter .begin() == .end()

Aren''t you checking to make sure your iteratores aren''t equal to .end()?

One of the most common uses of .begin() and .end() is in a for loop, such
as:

std::list<intData;
for ( std::list<int>::iterator it = Data.begin(); it != Data.end(); ++it )
{
// it is guaranteed to be pointing to an item in the list
}

Other uses include passing them to algorithms, also as result of .find()
etc..

But in all cases you must ensure that your iterator != .end()


说到迭代器:如果我初始化''iter = container.end()'',我可以

指望iter保持等于container.end()即使元素被删除并添加到容器中?
Speaking of iterators: if I initialize ''iter = container.end()'', can I
count on iter to stay equal to container.end() even if elements are
deleted and added to the container?



嗯......我不确定。我知道对容器的一些操作可以使
使迭代器无效。我很确定这包括.end(),其中
指向容器中最后一项之后的一个。


-

Jim Langston
ta*******@rocketmail.com


" Jim Langston" < ta ******* @ rocketmail.comwrites:
"Jim Langston" <ta*******@rocketmail.comwrites:

barcaroller写道:
barcaroller wrote:

>我注意到当容器为空时,container.begin()和container.end()都没有返回错误,所以当我取消引用迭代器时,我得到了一个令人讨厌的段错误。每次使用迭代器时,是否必须检查容器是否为空,或者我是否遗漏了某些东西?
>I''ve noticed that neither container.begin() nor container.end() return
an error when the container is empty, so I get a nasty segfault when I
dereference the iterator. Do I have to check if the container is
empty every time I use an iterator, or am I missing something?



你遗失了什么。对于空的容器.begin()==。end()


You are missing something. For an empty containter .begin() == .end()



更多,。end()始终是无效的迭代器,不能是$ /
derefenced!

Even more, .end() is always an invalid iterator that cannot be
derefenced!


[...]

但是在所有情况下你必须确保你的迭代器!= .end()
[...]
But in all cases you must ensure that your iterator != .end()

>说到迭代器:如果我初始化''iter = container.end()'',我是否可以依靠iter来保持等于container.end()即使元素被删除并添加到容器中?
>Speaking of iterators: if I initialize ''iter = container.end()'', can I
count on iter to stay equal to container.end() even if elements are
deleted and added to the container?



嗯......我不确定。我知道对容器的一些操作可以使
使迭代器无效。我很确定这包括.end(),其中
指向容器中最后一项之后的一个。


Hmm.. I''m not sure. I do know that some operations on containers can
invalidate iterators. I''m fairly sure that this includes .end() which
points one past the last item in the container.



是的,.end()的旧值可能变为有效,这将是无效的,

对于必须始终为的迭代器无效:-)


-

__Pascal Bourguignon__

Yes, the old value of .end() may become valid, which would be invalid,
for an iterator that must always be invalid :-)

--
__Pascal Bourguignon__


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

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