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