• <legend id='BoovI'><style id='BoovI'><dir id='BoovI'><q id='BoovI'></q></dir></style></legend>
      <bdo id='BoovI'></bdo><ul id='BoovI'></ul>

  • <i id='BoovI'><tr id='BoovI'><dt id='BoovI'><q id='BoovI'><span id='BoovI'><b id='BoovI'><form id='BoovI'><ins id='BoovI'></ins><ul id='BoovI'></ul><sub id='BoovI'></sub></form><legend id='BoovI'></legend><bdo id='BoovI'><pre id='BoovI'><center id='BoovI'></center></pre></bdo></b><th id='BoovI'></th></span></q></dt></tr></i><div id='BoovI'><tfoot id='BoovI'></tfoot><dl id='BoovI'><fieldset id='BoovI'></fieldset></dl></div>

      <small id='BoovI'></small><noframes id='BoovI'>

      <tfoot id='BoovI'></tfoot>

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

        C++ Segmentation when using erase on std::list(在 std::list 上使用擦除时的 C++ 分段)
          <tbody id='LmrmG'></tbody>
          <bdo id='LmrmG'></bdo><ul id='LmrmG'></ul>

          <i id='LmrmG'><tr id='LmrmG'><dt id='LmrmG'><q id='LmrmG'><span id='LmrmG'><b id='LmrmG'><form id='LmrmG'><ins id='LmrmG'></ins><ul id='LmrmG'></ul><sub id='LmrmG'></sub></form><legend id='LmrmG'></legend><bdo id='LmrmG'><pre id='LmrmG'><center id='LmrmG'></center></pre></bdo></b><th id='LmrmG'></th></span></q></dt></tr></i><div id='LmrmG'><tfoot id='LmrmG'></tfoot><dl id='LmrmG'><fieldset id='LmrmG'></fieldset></dl></div>

            <legend id='LmrmG'><style id='LmrmG'><dir id='LmrmG'><q id='LmrmG'></q></dir></style></legend>
          • <small id='LmrmG'></small><noframes id='LmrmG'>

                <tfoot id='LmrmG'></tfoot>

                  本文介绍了在 std::list 上使用擦除时的 C++ 分段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 erase 和列表迭代器从 C++ 链接列表中删除项目:

                  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.

                  推荐答案

                  这里的核心问题是您在调用 erase 之后使用迭代器值 it代码>就可以了.erase 方法使迭代器无效,因此继续使用它会导致不良行为.相反,您希望使用 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);
                  }
                  

                  检查 newList.end() 以解决 list 中至少没有 5 个元素的情况也没有坏处.

                  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);
                  }
                  

                  正如 Tim 所指出的,这里是 erase

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

                  • http://www.cplusplus.com/reference/stl/list/擦除/

                  这篇关于在 std::list 上使用擦除时的 C++ 分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)
                    <tbody id='NeusY'></tbody>

                  <small id='NeusY'></small><noframes id='NeusY'>

                    <legend id='NeusY'><style id='NeusY'><dir id='NeusY'><q id='NeusY'></q></dir></style></legend>

                          <tfoot id='NeusY'></tfoot>
                            <bdo id='NeusY'></bdo><ul id='NeusY'></ul>
                          • <i id='NeusY'><tr id='NeusY'><dt id='NeusY'><q id='NeusY'><span id='NeusY'><b id='NeusY'><form id='NeusY'><ins id='NeusY'></ins><ul id='NeusY'></ul><sub id='NeusY'></sub></form><legend id='NeusY'></legend><bdo id='NeusY'><pre id='NeusY'><center id='NeusY'></center></pre></bdo></b><th id='NeusY'></th></span></q></dt></tr></i><div id='NeusY'><tfoot id='NeusY'></tfoot><dl id='NeusY'><fieldset id='NeusY'></fieldset></dl></div>