<tfoot id='sNSSC'></tfoot>

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

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

      <legend id='sNSSC'><style id='sNSSC'><dir id='sNSSC'><q id='sNSSC'></q></dir></style></legend>
        <bdo id='sNSSC'></bdo><ul id='sNSSC'></ul>
    1. 列表迭代器 Remove()

      时间:2024-08-14

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

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

              • <tfoot id='Y9lSE'></tfoot>

                  <tbody id='Y9lSE'></tbody>
                <i id='Y9lSE'><tr id='Y9lSE'><dt id='Y9lSE'><q id='Y9lSE'><span id='Y9lSE'><b id='Y9lSE'><form id='Y9lSE'><ins id='Y9lSE'></ins><ul id='Y9lSE'></ul><sub id='Y9lSE'></sub></form><legend id='Y9lSE'></legend><bdo id='Y9lSE'><pre id='Y9lSE'><center id='Y9lSE'></center></pre></bdo></b><th id='Y9lSE'></th></span></q></dt></tr></i><div id='Y9lSE'><tfoot id='Y9lSE'></tfoot><dl id='Y9lSE'><fieldset id='Y9lSE'></fieldset></dl></div>
                本文介绍了列表迭代器 Remove()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个列表迭代器,它遍历列表并删除所有偶数.我可以使用列表迭代器很好地打印出数字,但我不能使用列表的 remove() 并传入取消引用的迭代器.

                I have a list iterator that goes through a list and removes all the even numbers. I can use the list iterator to print out the numbers fine but I cannot use the list's remove() and pass in the dereferenced iterator.

                我注意到当 remove() 语句生效时,*itr 被破坏了?有人能解释一下吗?

                I noticed that when the remove() statement is in effect, *itr gets corrupted? Can somebody explain this?

                #include <iostream>
                #include <list>
                
                #define MAX 100
                
                using namespace std;
                
                int main()
                {
                    list<int> listA;
                    list<int>::iterator itr;
                
                    //create list of 0 to 100
                    for(int i=0; i<=MAX; i++)
                        listA.push_back(i);
                
                    //remove even numbers
                    for(itr = listA.begin(); itr != listA.end(); ++itr)
                    {
                        if ( *itr % 2 == 0 )
                        {
                            cout << *itr << endl;
                            listA.remove(*itr);    //comment this line out and it will print properly
                        }
                    }
                }
                

                推荐答案

                您的上述代码存在一些问题.首先,remove 将使指向已删除元素的任何迭代器无效.然后继续使用迭代器.很难判断哪些元素 remove 在一般情况下会擦除(尽管不是在你的情况下),因为它可以删除多个.

                There are a few issues with your code above. Firstly, the remove will invalidate any iterators that are pointing at the removed elements. You then go on to continue using the iterator. It is difficult to tell which element(s) remove would erase in the general case (although not in yours) since it can remove more than one.

                其次,您可能使用了错误的方法.Remove 将遍历列表中的所有项目以查找任何匹配的元素 - 在您的情况下这将是低效的,因为只有一个.看起来您应该使用 erase 方法,您可能只想擦除迭代器位置的项目.erase 的好处是它返回一个位于下一个有效位置的迭代器.使用它的惯用方式是这样的:

                Secondly, you are probably using the wrong method. Remove will iterate through all of the items in the list looking for any matching elements - this will be inefficient in your case because there is only one. It looks like you should use the erase method, you probably only want to erase the item at the position of the iterator. The good thing about erase is it returns an iterator which is at the next valid position. The idiomatic way to use it is something like this:

                //remove even numbers
                for(itr = listA.begin(); itr != listA.end();)
                {
                    if ( *itr % 2 == 0 )
                    {
                        cout << *itr << endl;
                        itr=listA.erase(itr);
                    }
                    else
                      ++itr;
                }
                

                最后,你也可以使用 remove_if 来做你正在做的事情:

                Finally, you could also use remove_if to do the same as you are doing:

                bool even(int i) { return i % 2 == 0; }
                
                listA.remove_if(even);
                

                这篇关于列表迭代器 Remove()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:自定义类上的 STL 优先级队列 下一篇:c++ 将 find() 映射到可能的 insert():如何优化操作?

                相关文章

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

                      <bdo id='aqD6U'></bdo><ul id='aqD6U'></ul>
                  1. <tfoot id='aqD6U'></tfoot>

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

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