需要有关std :: list的帮助 [英] Need help regarding std::list

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

问题描述

好的,在我的程序中我有一个std :: list< Document *>,其中Document是我自己的一个
自己的类之一。


我需要查看此列表并检查每个项目是否已准备好删除

。如果不是,请跳到下一步,如果已经准备好,我会采取措施删除

。问题是列表是gui应用程序的一部分,系统

在执行循环体时从列表中删除Document *。

这意味着我不能使用迭代器迭代列表。所以我尝试使用

列表的大小,如下所示:


while(m_documents.size())

{

文件* ptr = m_documents.front();


if(ptr-> ready_for_deletion())

{

ptr-> clean_up(); //这会在下一步之前从列表中删除ptr

迭代

}

}


这个解决方案的问题是我得到了一个无限循环的文件

还没准备好删除。我需要确定我是否已经询问了文件是否已经准备好删除,并且在所有文件被删除之前循环或

只有列表中留下的非就绪文件。我想不出一个

的解决方案。我可以创建一个只有文件准备删除的新列表,然后

然后删除那些,但这不是一个选项,因为当我找到一个

文件准备好了删除,我需要立即将其删除,而不是稍后。


不确定我是否有意义,也许我不在话题。如果有的话,我道歉。


/ WP

解决方案

William Payne写道:

好的,在我的程序中我有一个std :: list< Document *>,其中Document是我自己的类之一。

我需要查看这个列表并且检查每个项目是否已准备好删除
。如果不是,请跳到下一步,如果已经准备就绪,我会采取措施删除它。问题是列表是gui应用程序的一部分,系统在执行循环体时从列表中删除Document *。
这意味着我不能使用迭代器迭代列表。


是的,你可以。


for(std :: list< Document *> :: iterator it = doclist.begin() ;

it!= doclist.end();

)//注意:没有++它

{

Document * pd = * it;

if(pd-> ready_for_deletion())

{

pd-> clean_up( );

删除pd;

it = doclist.erase(it);

}

else

++ it;

}


RTFM。

[...]




Victor




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

news:l6 **************** @ newsread1.dllstx09.us.to.v erio.net ...

William Payne写道:

好的,在我的程序中我有一个std :: list< Document *>,其中Document是我自己的类之一。

我需要查看此列表,并检查每个项目是否已准备好删除
。如果不是,请跳到下一步,如果已经准备就绪,我会采取措施删除它。问题是列表是gui应用程序的一部分,
系统在执行
循环体时从列表中删除Document *。这意味着我无法使用迭代器迭代列表。



是的,你可以。

for(std :: list< Document *> :: iterator it = doclist.begin();
it!= doclist.end();
)//注意:没有++它
{
文件* pd = *它;
if(pd-> ready_for_deletion())
{
pd-> clean_up();
删除pd;
it = doclist.erase(it );
}
其他
++ it;
}

RTFM。

[.. 。]



Victor




感谢您快速回复,Victor。有趣的解决方案。不幸的是,

实际删除了ptr_document;被称为其他地方(在gui库的

约束下工作),但我会看到我能用你的解决方案做什么。


/ WP


William Payne写道:

" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
新闻:l6 **************** @ newsread1.dllstx09.us.to.v erio.net ...

William Payne写道:

好的,在我的程序中我有一个std :: list< Document *>,其中Document是我自己的类之一。

我需要查看此列表并检查每个项目是否已准备好删除
。如果不是,请跳到下一步,如果已经准备就绪,我会采取措施删除它。问题是列表是gui应用程序的一部分,
系统在执行
循环体时从列表中删除Document *。这意味着我无法使用迭代器迭代列表。



是的,你可以。

for(std :: list< Document *> :: iterator it = doclist.begin();
it!= doclist.end();
)//注意:没有++它
{
文件* pd = *它;
if(pd-> ready_for_deletion())
{
pd-> clean_up();
删除pd;
it = doclist.erase(it );
}

++ it;
}

RTFM。

[...]



Victor



感谢您快速回复,Victor。有趣的解决方案。不幸的是,
实际删除了ptr_document;在其他地方被调用(在gui库的
约束下工作),但我会看到我可以用你的解决方案做什么。




你可以随时提取将要删除的列表元素分成

a单独列表,然后将其传递到其他地方进行处理。


Victor


Ok, in my program I have a std::list<Document*>, where Document is one of my
own classes.

I need to go through this list and check each item if it''s ready for
deletion. If it''s not, skip to next, if it''s ready I take steps to delete
it. The thing is that the list is part of a gui application and the system
removes the Document* from the list during the execution of the loop body.
That means I cant use iterators to iterate over the list. So I tried using
the size of the list, like this:

while(m_documents.size())
{
Document* ptr = m_documents.front();

if(ptr->ready_for_deletion())
{
ptr->clean_up(); // This removes ptr from the list before next
iteration
}
}

The problem with that solution is that I get an endless loop for Documents
not ready for deletion. I need to determine if I''ve asked a document if it''s
ready for deletion or not and loop until all documents have been deleted or
there are only non-ready documents left in the list. I can''t think of a
solution. I could create a new list of only Documents ready for deletion and
then delete those, but that''s not really an option because when I find a
document ready for deletion, I need to delete it right now, not a bit later.

Not sure I''m making much sense and maybe I''m off-topic. If so, I apologise.

/ WP

解决方案

William Payne wrote:

Ok, in my program I have a std::list<Document*>, where Document is one of my
own classes.

I need to go through this list and check each item if it''s ready for
deletion. If it''s not, skip to next, if it''s ready I take steps to delete
it. The thing is that the list is part of a gui application and the system
removes the Document* from the list during the execution of the loop body.
That means I cant use iterators to iterate over the list.
Yes, you can.

for (std::list<Document*>::iterator it = doclist.begin();
it != doclist.end();
) // note: no ++it
{
Document *pd = *it;
if (pd->ready_for_deletion())
{
pd->clean_up();
delete pd;
it = doclist.erase(it);
}
else
++it;
}

RTFM.
[...]



Victor



"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:l6****************@newsread1.dllstx09.us.to.v erio.net...

William Payne wrote:

Ok, in my program I have a std::list<Document*>, where Document is one of
my own classes.

I need to go through this list and check each item if it''s ready for
deletion. If it''s not, skip to next, if it''s ready I take steps to delete
it. The thing is that the list is part of a gui application and the
system removes the Document* from the list during the execution of the
loop body. That means I cant use iterators to iterate over the list.



Yes, you can.

for (std::list<Document*>::iterator it = doclist.begin();
it != doclist.end();
) // note: no ++it
{
Document *pd = *it;
if (pd->ready_for_deletion())
{
pd->clean_up();
delete pd;
it = doclist.erase(it);
}
else
++it;
}

RTFM.

[...]



Victor



Thanks for quick reply, Victor. Interesting solution. Unfortunately, the
actual delete ptr_document; is called somewhere else (working under the
constrains of gui library), but I will see what I can do with your solution.

/ WP


William Payne wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:l6****************@newsread1.dllstx09.us.to.v erio.net...

William Payne wrote:

Ok, in my program I have a std::list<Document*>, where Document is one of
my own classes.

I need to go through this list and check each item if it''s ready for
deletion. If it''s not, skip to next, if it''s ready I take steps to delete
it. The thing is that the list is part of a gui application and the
system removes the Document* from the list during the execution of the
loop body. That means I cant use iterators to iterate over the list.



Yes, you can.

for (std::list<Document*>::iterator it = doclist.begin();
it != doclist.end();
) // note: no ++it
{
Document *pd = *it;
if (pd->ready_for_deletion())
{
pd->clean_up();
delete pd;
it = doclist.erase(it);
}
else
++it;
}

RTFM.

[...]



Victor


Thanks for quick reply, Victor. Interesting solution. Unfortunately, the
actual delete ptr_document; is called somewhere else (working under the
constrains of gui library), but I will see what I can do with your solution.



You can always extract the elements of the list to be deleted into
a separate list and then pass it somewhere else to be disposed of.

Victor


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

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