• C++——list中erase和remove的区别


    1.之前在做相关的操作的时候,涉及到清除list相关的元素,因此会用到erase和remove,那么二者有什么区别呢?

    从官方文档中,我们可以获取以下信息

    erase :

    说明:Removes from the list container either a single element (position) or a range of elements ([first,last)).This effectively reduces the container size by the number of elements removed, which are destroyed.以iterator为单元,对元素进行清除。

    返回值:An iterator pointing to the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.

    remove:

    说明:Remove elements with specific value。Removes from the container all the elements that compare equal to val. This calls the destructor of these objects and reduces the container size by the number of elements removed.

    返回值:空

    erase是以迭代器为基本单位,清除元素,改变size的值;remove是以value相等为标准,也改变size的值。

    2.在清空list中,我们该用什么操作

    1     //调用析构函数,清掉了list的内存
    2     for (list<CUnit *>::iterator it = listStr.begin(); it != listStr.end(); )
    3     {
    4         delete *it;
    5         listStr.erase(it++);
    6         //listStr.remove(*it++);
    7     }
    8     listStr.clear();

    此处不要用remove,什么样的函数,就干什么样子的事情,别瞎用c++的函数。

    3.另外一个问题,为什么erase(it++)?

    从1中,我们可以看到erase的返回值是iterator。An iterator pointing to the element that followed the last element erased by the function call(指向erase元素的后一个元素的迭代器)。

    于是我们有了以下清除方法:

     1 #include"Allinclude.h"
     2 
     3 int main()
     4 {
     5     
     6     cout<<endl<<"map:"<<endl;
     7     map<char, int> mymap;
     8     // insert some values:
     9     mymap['a'] = 10;
    10     mymap['b'] = 20;
    11     mymap['c'] = 30;
    12     mymap['d'] = 40;
    13     mymap['e'] = 50;
    14     mymap['f'] = 60;
    15     for (map<char, int>::iterator it = mymap.begin(); it != mymap.end();)
    16     {
    17         #if 1
    18         if (it->second == 10)
    19         {
    20             //For the key - based version(2), the function returns the number of elements erased.
    21             mymap.erase(it++);
    22         }
    23         else
    24         {
    25             it++;
    26         }
    27         #endif
    28         //mymap.erase(it++);
    29     }
    30     for (map<char, int>::iterator it = mymap.begin(); it != mymap.end();it++)
    31     {
    32         cout << it->second << " , ";
    33     }
    34 
    35     cout<<endl<<"vector:"<<endl;
    36 
    37     vector<int> myvector;
    38 
    39     // set some values (from 1 to 10)
    40     for (int i = 1; i <= 10; i++)
    41         myvector.push_back(i);
    42     for (vector<int>::iterator it = myvector.begin(); it != myvector.end();)
    43     {
    44         #if 0
    45         if (*it == 5)
    46         {
    47             myvector.erase(it++);
    48             //等同于it=myvector.erase(it);因为返回值是下一个值的迭代器
    49         }
    50         else
    51         {
    52             it++;
    53         }
    54         #endif
    55         myvector.erase(it);
    56     }
    57 
    58     // set some values (from 1 to 10)
    59     for (int i = 0; i < myvector.size(); i++)
    60     {
    61         cout << myvector[i] << " , ";
    62     }
    63 
    64     cout<<endl<<"list:"<<endl;
    65     list<int> mylist;
    66 
    67     // set some values:
    68     for (int i = 1; i < 10; ++i)
    69         mylist.push_back(i * 10);
    70     for (list<int>::iterator it = mylist.begin(); it != mylist.end();)
    71     {
    72         #if 1
    73         if (*it == 50)
    74         {
    75             mylist.erase(it++);
    76             //等同于it=myvector.erase(it);因为返回值是下一个值的迭代器
    77             //it = mylist.erase(it);
    78         }
    79         else
    80         {
    81             it++;
    82         }
    83         #endif
    84         //mylist.erase(it++);
    85     }
    86 
    87     // set some values (from 1 to 10)
    88     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
    89     {
    90         cout << *it << " , ";
    91     }
    92 
    93     system("pause");
    94     return 0;
    95 }

    其实我建议还是用

    it = mylist.erase(it)

    代码更加清晰一点。

    其实最好还是用erase(begin(),end());除非要自己清除一些成员内存。

    参考文献:

    https://blog.csdn.net/liuzhi67/article/details/50950843

  • 相关阅读:
    编写Excel文件的Golang库
    Go多组Raft库
    Go GraphQL初学者教程
    简洁架构的思想,基于go实现
    gometalinter代码质量检查分析工具(golang)
    用go实现一个redis-cli
    Sublime text —— 自定义主题Soda
    Sublime text —— 自定义Color theme
    Sublime Text增加Build system类型,打造一个全能IDE
    一个简单的增强型PHP curl函数
  • 原文地址:https://www.cnblogs.com/whutao/p/10643344.html
Copyright © 2020-2023  润新知