當刪除一個STL容器(比如map, vector)中的某個元素時, 會引起迭代器失效, 所以, 我們務必提高警惕。
題目: 刪除map<int, int>中value為5的倍數的元素。 該題看起來很自然很簡單, 實則有迭代器失效的陷阱。
如果對迭代器失效問題一無所知, 則很容易寫出如下的錯誤代碼:
1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 typedef map<int, int> Map; 6 typedef map<int, int>::iterator MapIt; 7 8 void print(Map &m) 9 { 10 MapIt it; 11 for(it = m.begin(); it != m.end(); it++) 12 { 13 cout << it->second << " "; 14 } 15 16 cout << endl; 17 } 18 19 void deleteValueFromMap(Map &m, int n = 5) 20 { 21 MapIt it; 22 for(it = m.begin(); it != m.end(); it++) 23 { 24 if(0 == it->second % n) 25 { 26 m.erase(it); 27 } 28 } 29 } 30 31 int main() 32 { 33 Map m; 34 int i = 0; 35 for(i = 0; i < 21; i++) 36 { 37 m[i] = i; 38 } 39 40 print(m); 41 42 deleteValueFromMap(m); // 程序崩潰 43 44 return 0; 45 }
運行上述程序, 結果程序崩潰,什么原因呢? 原來, 對於關聯的容器map來說, m.erase(it);后, it就失效了, 而for循環中有it++, 自然而然會出問題啊。 那怎么辦呢? 且看:
1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 typedef map<int, int> Map; 6 typedef map<int, int>::iterator MapIt; 7 8 void print(Map &m) 9 { 10 MapIt it; 11 for(it = m.begin(); it != m.end(); it++) 12 { 13 cout << it->second << " "; 14 } 15 16 cout << endl; 17 } 18 19 void deleteValueFromMap(Map &m, int n = 5) 20 { 21 MapIt it; 22 MapIt tmp; 23 for(it = m.begin(); it != m.end(); /*不能再自增了*/) 24 { 25 if(0 == it->second % n) 26 { 27 tmp = it++; 28 m.erase(tmp); 29 } 30 else 31 { 32 it++; 33 } 34 } 35 } 36 37 int main() 38 { 39 Map m; 40 int i = 0; 41 for(i = 0; i < 21; i++) 42 { 43 m[i] = i; 44 } 45 46 print(m); 47 48 deleteValueFromMap(m); // 程序ok 49 print(m); 50 51 return 0; 52 } 53 結果為: 54 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 55 1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 56 57 58 59 60 61 當然, 上述程序也可以繼續簡化為: 62 63 64 65 #include <iostream> 66 #include <map> 67 using namespace std; 68 69 typedef map<int, int> Map; 70 typedef map<int, int>::iterator MapIt; 71 72 void print(Map &m) 73 { 74 MapIt it; 75 for(it = m.begin(); it != m.end(); it++) 76 { 77 cout << it->second << " "; 78 } 79 80 cout << endl; 81 } 82 83 void deleteValueFromMap(Map &m, int n = 5) 84 { 85 MapIt it; 86 for(it = m.begin(); it != m.end(); /*不能再自增了*/) 87 { 88 if(0 == it->second % n) 89 { 90 m.erase(it++); 91 } 92 else 93 { 94 it++; 95 } 96 } 97 } 98 99 int main() 100 { 101 Map m; 102 int i = 0; 103 for(i = 0; i < 21; i++) 104 { 105 m[i] = i; 106 } 107 108 print(m); 109 110 deleteValueFromMap(m); // 程序ok 111 print(m); 112 113 return 0; 114 } 115 結果ok.
有的朋友肯定會問, m.erase(it++);就不會產生迭代器失效么? 確實不會! 為什么呢? 這樣從it++說起, 為了簡便起見, 我們用p++來代替吧。 看程序:
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char szTest[] = "abcdefg"; 7 char *p = szTest; 8 9 cout << *p++ << endl; 10 11 return 0; 12 }
大家都知道, 結果為a. 但是, 很多人錯誤地以為是先執行*p, 然后執行p++, 其實, 這是個天大的誤解。 大家可以查一下*和++的執行順序, 雖然*和++的優先級相同, 但此處采取的是右結合方式, 實際上先執行的是p++, 不過, p++的返回值是原來的p, 也就是說, *p++的值還是a.
所以, 在m.erase(it++);中,it++先執行, 此時還沒有erase, 程序自然不會崩潰. 當it++執行完后, 已經指向了下一個元素了, 但it++的返回值還是當前元素, 此時再刪除它, 合情合理。
OK, map的迭代器失效問題,先介紹到這里。
那vector又是怎樣的現象呢? 看程序:
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 typedef vector<int> Vec; 6 typedef vector<int>::iterator VecIt; 7 8 void print(Vec &v) 9 { 10 VecIt it; 11 for(it = v.begin(); it != v.end(); it++) 12 { 13 cout << *it << " "; 14 } 15 16 cout << endl; 17 } 18 19 void deleteValueFromVector(Vec &v, int n = 5) 20 { 21 VecIt it; 22 for(it = v.begin(); it != v.end(); it++) 23 { 24 if(0 == *it % n) 25 { 26 v.erase(it); 27 } 28 } 29 } 30 31 int main() 32 { 33 Vec v; 34 int i = 0; 35 for(i = 0; i < 21; i++) 36 { 37 v.push_back(i); // 不能再傻傻地用下標了 38 } 39 40 print(v); 41 42 deleteValueFromVector(v); // 程序崩潰 43 print(v); 44 45 return 0; 46 } 47 運行程序, 結果程序也崩潰, 可見, vector也存在迭代器失效的問題, 怎么改呢? 如下: 48 49 50 51 52 53 #include <iostream> 54 #include <vector> 55 using namespace std; 56 57 typedef vector<int> Vec; 58 typedef vector<int>::iterator VecIt; 59 60 void print(Vec &v) 61 { 62 VecIt it; 63 for(it = v.begin(); it != v.end(); it++) 64 { 65 cout << *it << " "; 66 } 67 68 cout << endl; 69 } 70 71 void deleteValueFromVector(Vec &v, int n = 5) 72 { 73 VecIt it; 74 for(it = v.begin(); it != v.end(); /*不能再自增了*/) 75 { 76 if(0 == *it % n) 77 { 78 v.erase(it); // vector元素自動向前挪動了(關聯的map容器元素不會自動挪動), 所以不能再把it進行++了 79 } 80 else 81 { 82 it++; 83 } 84 } 85 } 86 87 int main() 88 { 89 Vec v; 90 int i = 0; 91 for(i = 0; i < 21; i++) 92 { 93 v.push_back(i); // 不能再傻傻地用下標了 94 } 95 96 print(v); 97 98 deleteValueFromVector(v); // 程序ok 99 print(v); 100 101 return 0; 102 } 103 結果為: 104 105 106 107 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 108 1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 109 110 可見, vector迭代器失效的問題也不容忽視。
總之, 下次說到迭代器失效(尤其涉及到容器大小的改變, 比如刪除, 插入)時, 一定要留個心眼。 在實際編程中, 如果稍不注意, 則可能引起非常難以捕捉的bug, 折磨你我, 何不提前防范呢?