C++在 std::list 上使用擦除时的分段

发布于 2024-10-19 11:50:21 字数 1023 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兮子 2024-10-26 11:50:21

这里的核心问题是,在调用了 erase 之后,您正在使用迭代器值 iterase 方法会使迭代器失效,因此继续使用它会导致不良行为。相反,您希望使用 erase 的返回值来获取擦除值之后的下一个有效迭代器。

it = newList.begin();
for (int i = 0; i < 5; i++) {
  it = newList.erase(it);
}

包含对 newList.end() 的检查来解决 list 中没有至少 5 个元素的情况也没有什么坏处。

it = newList.begin();
for (int i = 0; i < 5 && it != newList.end(); i++) {
  it = newList.erase(it);
}

正如 Tim 指出的,这里有一个关于erase的很好的参考

The core problem here is you're using at iterator value, it, after you've called erase on it. The erase method invalidates the iterator and hence continuing to use it results in bad behavior. Instead you want to use the return of erase to get the next valid iterator after the erased value.

it = newList.begin();
for (int i = 0; i < 5; i++) {
  it = newList.erase(it);
}

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 the list.

it = newList.begin();
for (int i = 0; i < 5 && it != newList.end(); i++) {
  it = newList.erase(it);
}

As Tim pointed out, here's a great reference for erase

李不 2024-10-26 11:50:21

当您删除 it 位置的元素时,迭代器 it 就会失效 - 它指向您刚刚释放的一块内存。

erase(it) 函数返回另一个指向列表中下一个元素的迭代器。用那个吧!

When you erase an element at position it, the iterator it 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!

日记撕了你也走了 2024-10-26 11:50:21

当您在循环中 erase() 时,您的迭代器就会失效。做这样的事情来代替擦除循环会更简单:

list_item_t::iterator endIter = newlist.begin();
std::advance(endIter, 5);
newList.erase(newlist.begin(), endIter);

您可能还对 感兴趣擦除-删除习语

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:

list_item_t::iterator endIter = newlist.begin();
std::advance(endIter, 5);
newList.erase(newlist.begin(), endIter);

You might also be interested in the erase-remove idiom.

对不⑦ 2024-10-26 11:50:21

我这样做:

for(list<type>::iterator i = list.begin(); i != list.end(); i++)
{
     if(shouldErase)
     { 
        i = list.erase(i);
        i--;
     }
}

编辑,因为我是个笨蛋,显然无法阅读哈哈。

I do this:

for(list<type>::iterator i = list.begin(); i != list.end(); i++)
{
     if(shouldErase)
     { 
        i = list.erase(i);
        i--;
     }
}

Edited because I'm a bonehead that can't read apparently lol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文