需要一些关于list :: end()的帮助 [英] need some help with list::end ()

查看:65
本文介绍了需要一些关于list :: end()的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用stl的列表存储一些文件名。它声明为:


list< char * filenames;

我使用以下方式枚举列表:


list< char *> :: const_iterator文件名;


filename = filenames.begin();


比我只是做一个文件名++去下一个项目.....


我在我的代码中的* if *条件中检查filenames.end()

检查我是否我正在处理清单的最后一项。执行此操作的代码是

正常工作。但是现在由于我的代码发生了一些变化,在* filenames.end()检查之前删除了一些列表项

。我注意到这个

检查现在永远不会评估为真,虽然我在调试器中知道我是站在最后一项的
。我确定我在这里错过了一些基本概念。我现在可以检查这是列表中的最后一项吗?


我正在使用vc ++ 2k5总不受管理。


问候,


-ab。

Hi all,

I''m using stl''s list to store some file names. Its declared as:

list < char * filenames;
i enumerate the list by using:

list < char * >::const_iterator filename;

filename = filenames.begin();

than I simply do a filename++ to go to the next item.....

I check for filenames.end() in an *if* condition somewhere in my code to
check if I''m dealing with the last item of the list. The code to do this was
working fine. But now due to some change in my code, some of the list items
are deleted *before* the filenames.end() check. I have noticed that this
check now never evaluates to true although I know in the debugger that I''m
standing at the last item. I''m sure I''m missing some basic concept here. How
can I check now that this is the last item of the list?

I''m using vc++ 2k5 total unmanaged.

Regards,

-ab.

推荐答案



阿布巴卡尔 < em ********** @ yahoo.comskrev i meddelandet

news:up ************** @ TK2MSFTNGP03.phx.gbl ...

"Abubakar" <em**********@yahoo.comskrev i meddelandet
news:up**************@TK2MSFTNGP03.phx.gbl...

大家好,


我正在使用stl的列表存储一些文件名。它声明为:


list< char * filenames;

我使用以下方式枚举列表:


list< char *> :: const_iterator文件名;


filename = filenames.begin();


比我只是做一个文件名++去下一个项目.....


我在我的

代码中的某个* if *条件中检查filenames.end()为

检查我是否正在处理列表的最后一项。做的代码

这是

工作正常。但是现在由于我的代码发生了一些变化,在* filenames.end()检查之前删除了一些

列表项

。我注意到了这个

现在检查现在永远不会评估为真虽然我在调试器中知道

我是

站在最后一项。我确定我错过了一些基本的概念

。我现在可以检查这是列表的最后一项吗?
Hi all,

I''m using stl''s list to store some file names. Its declared as:

list < char * filenames;
i enumerate the list by using:

list < char * >::const_iterator filename;

filename = filenames.begin();

than I simply do a filename++ to go to the next item.....

I check for filenames.end() in an *if* condition somewhere in my
code to
check if I''m dealing with the last item of the list. The code to do
this was
working fine. But now due to some change in my code, some of the
list items
are deleted *before* the filenames.end() check. I have noticed that
this
check now never evaluates to true although I know in the debugger
that I''m
standing at the last item. I''m sure I''m missing some basic concept
here. How
can I check now that this is the last item of the list?



如何删除这些项目?只做filenames.erase(filename)

使删除对象的迭代器无效。


你应该做的事情如


if(some_condition)

filename = filenames.erase(filename);

else

++ filename;


这将* *使用返回的迭代器到下一个对象,或者

递增当前迭代器。你应该做一个,但不是两个! :-)

Bo Persson

How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

You should be doing something like

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

This will *either* use the returned iterator to the next object, or
increment the current iterator. You should do one, but not both! :-)
Bo Persson


对于迟到的回复抱歉。
Sorry for the late reply.

你是如何删除这些项目的?只做filenames.erase(filename)

使删除对象的迭代器无效。
How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.



我使用删除
我有以下声明:


list< char *> :: const_iterator name,prev;


在某些时候我这样做:


prev = name;

name ++;

filenames .remove(*上限);


这就是我删除这些物品的方式。


问候,

-ab。

" Bo Persson" < bo *@gmb.dkwrote in message

news:4k ************ @ individual.net ...

I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.

regards,
-ab.
"Bo Persson" <bo*@gmb.dkwrote in message
news:4k************@individual.net...


>

" Abubakar" < em ********** @ yahoo.comskrev i meddelandet

news:up ************** @ TK2MSFTNGP03.phx.gbl ...
>
"Abubakar" <em**********@yahoo.comskrev i meddelandet
news:up**************@TK2MSFTNGP03.phx.gbl...

大家好,


我正在使用stl的列表存储一些文件名。它声明为:


list< char * filenames;

我使用以下方式枚举列表:


list< char *> :: const_iterator文件名;


filename = filenames.begin();


比我只是做一个文件名++去下一个项目.....


我在我的

代码中的某个* if *条件中检查filenames.end()为

检查我是否正在处理列表的最后一项。做的代码

这是

工作正常。但是现在由于我的代码发生了一些变化,在* filenames.end()检查之前删除了一些

列表项

。我注意到了这个

现在检查现在永远不会评估为真虽然我在调试器中知道

我是

站在最后一项。我确定我错过了一些基本的概念

。我现在可以检查这是列表中的最后一项吗?
Hi all,

I''m using stl''s list to store some file names. Its declared as:

list < char * filenames;
i enumerate the list by using:

list < char * >::const_iterator filename;

filename = filenames.begin();

than I simply do a filename++ to go to the next item.....

I check for filenames.end() in an *if* condition somewhere in my
code to
check if I''m dealing with the last item of the list. The code to do
this was
working fine. But now due to some change in my code, some of the
list items
are deleted *before* the filenames.end() check. I have noticed that
this
check now never evaluates to true although I know in the debugger
that I''m
standing at the last item. I''m sure I''m missing some basic concept
here. How
can I check now that this is the last item of the list?



你如何删除这些物品?只做filenames.erase(filename)

使删除对象的迭代器无效。


你应该做的事情如


if(some_condition)

filename = filenames.erase(filename);

else

++ filename;


这将* *使用返回的迭代器到下一个对象,或者

递增当前迭代器。你应该做一个,但不是两个! :-)


Bo Persson


How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

You should be doing something like

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

This will *either* use the returned iterator to the next object, or
increment the current iterator. You should do one, but not both! :-)
Bo Persson



Abubakar写道:
Abubakar wrote:

很抱歉迟到的回复。
Sorry for the late reply.

>你如何删除这些项目?只做filenames.erase(filename)
使删除对象的迭代器无效。
>How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.



我使用删除
我有以下声明:


list< char *> :: const_iterator name,prev;


在某些时候我这样做:


prev = name;

name ++;

filenames .remove(*上限);


这就是我删除这些项目的方法。


I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.



如果您的列表中有重复项,则删除调用将删除比其中一个更多的

,包括可能的一个现在由

" name"引用。使用Bo建议的循环:


if(some_condition)

filename = filenames.erase(filename);

else

++文件名;


这是通过列表删除某些元素的规范方式,因为

你去了。


-cd

If you have duplicates in your list, the call to remove will remove more
than one of them at a time, including possibly the one now referenced by
"name". Use the loop that Bo suggested:

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

that''s the canonical way to iterate through a list removing some elements as
you go.

-cd


这篇关于需要一些关于list :: end()的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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