从std :: list中删除项目 [英] Deleting items from std::list

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

问题描述




STL似乎缺少一些功能。


我正在迭代链表(std: :list)使用反向

迭代器并尝试从列表中删除某些项目。重要的是我向后遍历列表,因为在删除之前必须以相反的顺序处理其中的项目

。但是,

似乎没有为

反向迭代器定义的std :: list :: erase()方法。


我正在使用STLport 4.5和Borland C ++ Builder 6.


以下示例代码显示了我想要做的事情:


/ /要从列表中删除的任意值

const int VALUE_TO_ERASE = 12;


std :: list< int>数据;


// [这里用值加载数据的代码]


std :: list< int> :: reverse_iterator ri = data .rbegin();


while(ri!= data.rend())

{

//获取下一个迭代器如果ri被删除。

std :: list< int> :: reverse_iterator next = ri;

++ next;


//检查ri是否需要删除

if(* ri == VALUE_TO_ERASE)

{

// [代码此处为擦除前处理* ri]

data.erase(ri); //< - 导致编译错误

}


ri = next;

}

显然,在
std :: list中没有为反向迭代器定义擦除方法。


这个问题有一个很好的解决方案吗?我需要获得更多

最新版本的STLport吗?


谢谢,

Markus。

解决方案

* Markus Svilans:


显然,
中没有为反向迭代器定义擦除方法std :: list。

这个问题有一个很好的解决方案吗?




反转列表(调用data.reverse()),然后在前进

方向迭代。


-

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

问:为什么这么糟糕?

A:热门帖子。

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




Alf P. Steinbach写道:

* Markus Svilans:< blockquote class =post_quotes>
显然,
std :: list中的反向迭代器没有定义擦除方法。

是否有一个很好的sol这个问题?



反转列表(调用data.reverse()),然后向前方向迭代。




感谢您的回复。


如果列表在处理之前被撤消,然后再次转回

,这只会简短的合理解决方案

列表。但是我有时候需要处理几百元b $ b元素的列表。


假设在处理之前无法反转列表,那么

还有其他选择吗?


谢谢,

马库斯。

* Markus Svilans:

Alf P. Steinbach写道:

* Markus Svilans:

显然,擦除方法没有定义在
std :: list中反转迭代器。

这个问题有一个很好的解决方案吗?颠倒列表(调用data.reverse()),然后向前方向迭代。



感谢您的回复。

如果列表在处理之前要被撤销,然后再转回
,这只是简短的
列表的合理解决方案。




可以'不明白为什么;无论如何,你正在遍历完整的列表。


但是我有时必须处理具有几百个元素的列表。


那是非常短的(不是长度很重要,因为你需要通过完整的清单来获得
)。

假设在处理之前无法撤销列表,


它是。


有没有其他选择?




是的。您可以在开始时添加虚拟项目。然后你可以使用

reverse_iterator :: base()。但这既复杂又低效。


免责声明:可能有一种我不知道的简单方法,我不会使用

这个东西定期。


-

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

Q :为什么这么糟糕?

A:热门帖子。

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


Hi,

There seems to be some functionality missing from the STL.

I am iterating through a linked list (std::list) using a reverse
iterator and attempting to erase certain items from the list. It is
important that I iterate through the list backwards, because the items
in it have to be processed in reverse order before erasing. However,
there does not appear to be an std::list::erase() method defined for
reverse iterators.

I am using STLport 4.5 with Borland C++ Builder 6.

The following example code shows what I am trying to do:

// Arbitrary value to remove from the list
const int VALUE_TO_ERASE = 12;

std::list<int> data;

// [ code here to load data with values ]

std::list<int>::reverse_iterator ri = data.rbegin();

while (ri != data.rend())
{
// Get next iterator in case ri is erased.
std::list<int>::reverse_iterator next = ri;
++next;

// Check if ri needs to be erased
if (*ri == VALUE_TO_ERASE)
{
// [ code here to process *ri before erasing ]
data.erase(ri); // <-- Causes compiler error
}

ri = next;
}

Obviously an erase method is not defined for reverse iterators in
std::list.

Is there a nice solution to this problem? Do I need to get a more
recent version of STLport?

Thanks,
Markus.

解决方案

* Markus Svilans:


Obviously an erase method is not defined for reverse iterators in
std::list.

Is there a nice solution to this problem?



Reverse the list (call data.reverse()), then iterate in the forward
direction.

--
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 wrote:

* Markus Svilans:


Obviously an erase method is not defined for reverse iterators in
std::list.

Is there a nice solution to this problem?



Reverse the list (call data.reverse()), then iterate in the forward
direction.



Thanks for your response.

If the list were to be reversed before processing, and then reversed
again afterwards, this would only be reasonable solution for short
lists. However I sometimes have to deal with lists with a few hundred
elements.

Assuming it''s not possible to reverse the list before processing, are
there any alternatives?

Thanks,
Markus.


* Markus Svilans:

Alf P. Steinbach wrote:

* Markus Svilans:

Obviously an erase method is not defined for reverse iterators in
std::list.

Is there a nice solution to this problem? Reverse the list (call data.reverse()), then iterate in the forward
direction.



Thanks for your response.

If the list were to be reversed before processing, and then reversed
again afterwards, this would only be reasonable solution for short
lists.



Can''t see why; you''re traversing the complete list anyway.

However I sometimes have to deal with lists with a few hundred
elements.
That''s extremely short (not that the length matters since you''re
traversing the complete list).

Assuming it''s not possible to reverse the list before processing,
It is.

are there any alternatives?



Yes. You can add a dummy item at the start. Then you can use
reverse_iterator::base(). But that is both complex and inefficient.

Disclaimer: there may a simpler way that I don''t know about, I don''t use
this stuff regularly.

--
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?


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

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