C++在 std::list 上使用擦除时的分段
我正在尝试使用 erase
和列表迭代器从 C++ 链接列表中删除项目:
#include <iostream>
#include <string>
#include <list>
class Item
{
public:
Item() {}
~Item() {}
};
typedef std::list<Item> list_item_t;
int main(int argc, const char *argv[])
{
// create a list and add items
list_item_t newlist;
for ( int i = 0 ; i < 10 ; ++i )
{
Item temp;
newlist.push_back(temp);
std::cout << "added item #" << i << std::endl;
}
// delete some items
int count = 0;
list_item_t::iterator it;
for ( it = newlist.begin(); count < 5 ; ++it )
{
std::cout << "round #" << count << std::endl;
newlist.erase( it );
++count;
}
return 0;
}
我得到此输出,但似乎无法追踪原因:
added item #0
added item #1
added item #2
added item #3
added item #4
added item #5
added item #6
added item #7
added item #8
added item #9
round #0
round #1
Segmentation fault
我可能做错了,但会无论如何,感谢帮助。谢谢。
I'm trying to remove items from a C++ linked list using erase
and a list iterator:
#include <iostream>
#include <string>
#include <list>
class Item
{
public:
Item() {}
~Item() {}
};
typedef std::list<Item> list_item_t;
int main(int argc, const char *argv[])
{
// create a list and add items
list_item_t newlist;
for ( int i = 0 ; i < 10 ; ++i )
{
Item temp;
newlist.push_back(temp);
std::cout << "added item #" << i << std::endl;
}
// delete some items
int count = 0;
list_item_t::iterator it;
for ( it = newlist.begin(); count < 5 ; ++it )
{
std::cout << "round #" << count << std::endl;
newlist.erase( it );
++count;
}
return 0;
}
I get this output and can't seem to trace the reason:
added item #0
added item #1
added item #2
added item #3
added item #4
added item #5
added item #6
added item #7
added item #8
added item #9
round #0
round #1
Segmentation fault
I'm probably doing it wrong, but would appreciate help anyway. thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的核心问题是,在调用了
erase
之后,您正在使用迭代器值it
。erase
方法会使迭代器失效,因此继续使用它会导致不良行为。相反,您希望使用erase
的返回值来获取擦除值之后的下一个有效迭代器。包含对
newList.end()
的检查来解决list
中没有至少 5 个元素的情况也没有什么坏处。正如 Tim 指出的,这里有一个关于
erase
的很好的参考The core problem here is you're using at iterator value,
it
, after you've callederase
on it. Theerase
method invalidates the iterator and hence continuing to use it results in bad behavior. Instead you want to use the return oferase
to get the next valid iterator after the erased value.It also doesn't hurt to include a check for
newList.end()
to account for the case where there aren't at least 5 elements in thelist
.As Tim pointed out, here's a great reference for
erase
当您删除
it
位置的元素时,迭代器it
就会失效 - 它指向您刚刚释放的一块内存。erase(it)
函数返回另一个指向列表中下一个元素的迭代器。用那个吧!When you erase an element at position
it
, the iteratorit
gets invalidated - it points to a piece of memory that you just freed.The
erase(it)
function returns another iterator pointing to the next element to the list. Use that one!当您在循环中
erase()
时,您的迭代器就会失效。做这样的事情来代替擦除循环会更简单:您可能还对 感兴趣擦除-删除习语。
You're invalidating your iterator when you
erase()
within the loop. It would be simpler to do something like this in place of your erase loop:You might also be interested in the erase-remove idiom.
我这样做:
编辑,因为我是个笨蛋,显然无法阅读哈哈。
I do this:
Edited because I'm a bonehead that can't read apparently lol.