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

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

      • <bdo id='L5bXi'></bdo><ul id='L5bXi'></ul>

      <legend id='L5bXi'><style id='L5bXi'><dir id='L5bXi'><q id='L5bXi'></q></dir></style></legend>
      1. 如果在从头到尾迭代时在 map 元素上调用 erase() 会发生什么?

        时间:2024-05-12

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

            • <small id='Lib6A'></small><noframes id='Lib6A'>

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

                  <tbody id='Lib6A'></tbody>

                  本文介绍了如果在从头到尾迭代时在 map 元素上调用 erase() 会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在下面的代码中,我遍历地图并测试是否需要删除元素.擦除元素并继续迭代是否安全,或者我是否需要在另一个容器中收集密钥并执行第二个循环来调用 erase()?

                  In the following code I loop through a map and test if an element needs to be erased. Is it safe to erase the element and keep iterating or do I need to collect the keys in another container and do a second loop to call the erase()?

                  map<string, SerialdMsg::SerialFunction_t>::iterator pm_it;
                  for (pm_it = port_map.begin(); pm_it != port_map.end(); pm_it++)
                  {
                      if (pm_it->second == delete_this_id) {
                          port_map.erase(pm_it->first);
                      }
                  }
                  

                  更新:当然,然后我阅读这个问题,但我没有认为会相关,但回答了我的问题.

                  UPDATE: Of course, I then read this question which I didn't think would be related but answers my question.

                  推荐答案

                  C++11

                  这已在 C++11 中得到修复(或擦除已在所有容器类型中得到改进/保持一致).
                  擦除方法现在返回下一个迭代器.

                  C++11

                  This has been fixed in C++11 (or erase has been improved/made consistent across all container types).
                  The erase method now returns the next iterator.

                  auto pm_it = port_map.begin();
                  while(pm_it != port_map.end())
                  {
                      if (pm_it->second == delete_this_id)
                      {
                          pm_it = port_map.erase(pm_it);
                      }
                      else
                      {
                          ++pm_it;
                      }
                  }
                  

                  C++03

                  擦除地图中的元素不会使任何迭代器失效.
                  (除了被删除元素的迭代器)

                  C++03

                  Erasing elements in a map does not invalidate any iterators.
                  (apart from iterators on the element that was deleted)

                  实际上插入或删除不会使任何迭代器失效:

                  Actually inserting or deleting does not invalidate any of the iterators:

                  另见这个答案:
                  马克勒索技术

                  但您确实需要更新您的代码:
                  在您的代码中,您在调用擦除后增加 pm_it.此时为时已晚,已经失效.

                  But you do need to update your code:
                  In your code you increment pm_it after calling erase. At this point it is too late and is already invalidated.

                  map<string, SerialdMsg::SerialFunction_t>::iterator pm_it = port_map.begin();
                  while(pm_it != port_map.end())
                  {
                      if (pm_it->second == delete_this_id)
                      {
                          port_map.erase(pm_it++);  // Use iterator.
                                                    // Note the post increment.
                                                    // Increments the iterator but returns the
                                                    // original value for use by erase 
                      }
                      else
                      {
                          ++pm_it;           // Can use pre-increment in this case
                                             // To make sure you have the efficient version
                      }
                  }
                  

                  这篇关于如果在从头到尾迭代时在 map 元素上调用 erase() 会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么标准迭代器范围是 [begin, end) 而不是 [begin, end]? 下一篇:模板专业化 VS 函数重载

                  相关文章

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

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

                    <bdo id='GzcWo'></bdo><ul id='GzcWo'></ul>

                    1. <tfoot id='GzcWo'></tfoot>