map和vector的迭代器失效问题(某公司招聘笔试试题)
生活随笔
收集整理的這篇文章主要介紹了
map和vector的迭代器失效问题(某公司招聘笔试试题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當刪除一個STL容器(比如map, vector)中的某個元素時, 會引起迭代器失效, 所以, 我們務必提高警惕。 某次筆試, 我遇到這樣一個題目: 刪除map<int, int>中value為5的倍數的元素。 該題看起來很自然很簡單, 實則有迭代器失效的陷阱。
如果對迭代器失效問題一無所知, 則很容易寫出如下的錯誤代碼: #include <iostream> #include <map> using namespace std;typedef map<int, int> Map; typedef map<int, int>::iterator MapIt;void print(Map &m) {MapIt it;for(it = m.begin(); it != m.end(); it++){cout << it->second << " ";}cout << endl; }void deleteValueFromMap(Map &m, int n = 5) {MapIt it;for(it = m.begin(); it != m.end(); it++){if(0 == it->second % n){m.erase(it);}} }int main() {Map m;int i = 0;for(i = 0; i < 21; i++){m[i] = i;}print(m);deleteValueFromMap(m); // 程序崩潰return 0; }
運行上述程序, 結果程序崩潰, 什么原因呢? 原來, 對于關聯的容器map來說, m.erase(it);后, it就失效了, 而for循環中有it++, 自然而然會出問題啊。 那怎么辦呢? 且看: #include <iostream> #include <map> using namespace std;typedef map<int, int> Map; typedef map<int, int>::iterator MapIt;void print(Map &m) {MapIt it;for(it = m.begin(); it != m.end(); it++){cout << it->second << " ";}cout << endl; }void deleteValueFromMap(Map &m, int n = 5) {MapIt it;MapIt tmp;for(it = m.begin(); it != m.end(); /*不能再自增了*/){if(0 == it->second % n){tmp = it++;m.erase(tmp);}else{it++;}} }int main() {Map m;int i = 0;for(i = 0; i < 21; i++){m[i] = i;}print(m);deleteValueFromMap(m); // 程序okprint(m);return 0; }
結果為:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
當然, 上述程序也可以繼續簡化為: #include <iostream> #include <map> using namespace std;typedef map<int, int> Map; typedef map<int, int>::iterator MapIt;void print(Map &m) {MapIt it;for(it = m.begin(); it != m.end(); it++){cout << it->second << " ";}cout << endl; }void deleteValueFromMap(Map &m, int n = 5) {MapIt it;for(it = m.begin(); it != m.end(); /*不能再自增了*/){if(0 == it->second % n){m.erase(it++);}else{it++;}} }int main() {Map m;int i = 0;for(i = 0; i < 21; i++){m[i] = i;}print(m);deleteValueFromMap(m); // 程序okprint(m);return 0; }
結果ok.
有的朋友肯定會問, m.erase(it++);就不會產生迭代器失效么? 確實不會! 為什么呢? 這樣從it++說起, 為了簡便起見, 我們用p++來代替吧。 看程序: #include <iostream> using namespace std;int main() {char szTest[] = "abcdefg";char *p = szTest;cout << *p++ << endl;return 0; }
大家都知道, 結果為a. 但是, 很多人錯誤地以為是先執行*p, 然后執行p++, 其實, 這是個天大的誤解。 大家可以查一下*和++的執行順序, 雖然*和++的優先級相同, 但此處采取的是右結合方式, 實際上先執行的是p++, 不過, p++的返回值是原來的p, 也就是說, *p++的值還是a.
所以, 在m.erase(it++);中,it++先執行, 此時還沒有erase, 程序自然不會崩潰. 當it++執行完后, 已經指向了下一個元素了, 但it++的返回值還是當前元素, 此時再刪除它, 合情合理。
OK, map的迭代器失效問題, 我們先介紹到這里。
那vector又是怎樣的現象呢? 看程序: #include <iostream> #include <vector> using namespace std;typedef vector<int> Vec; typedef vector<int>::iterator VecIt;void print(Vec &v) {VecIt it;for(it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl; }void deleteValueFromVector(Vec &v, int n = 5) {VecIt it;for(it = v.begin(); it != v.end(); it++){if(0 == *it % n){v.erase(it);}} }int main() {Vec v;int i = 0;for(i = 0; i < 21; i++){v.push_back(i); // 不能再傻傻地用下標了}print(v);deleteValueFromVector(v); // 程序崩潰print(v);return 0; }
運行程序, 結果程序也崩潰, 可見, vector也存在迭代器失效的問題, 怎么改呢? 如下: #include <iostream> #include <vector> using namespace std;typedef vector<int> Vec; typedef vector<int>::iterator VecIt;void print(Vec &v) {VecIt it;for(it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl; }void deleteValueFromVector(Vec &v, int n = 5) {VecIt it;for(it = v.begin(); it != v.end(); /*不能再自增了*/){if(0 == *it % n){v.erase(it); // vector元素自動向前挪動了(關聯的map容器元素不會自動挪動), 所以不能再把it進行++了}else{it++;}} }int main() {Vec v;int i = 0;for(i = 0; i < 21; i++){v.push_back(i); // 不能再傻傻地用下標了}print(v);deleteValueFromVector(v); // 程序okprint(v);return 0; }
結果為:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
可見, vector迭代器失效的問題也不容忽視。
總之, 下次說到迭代器失效(尤其涉及到容器大小的改變, 比如刪除, 插入)時, 一定要留個心眼。 在實際編程中, 如果稍不注意, 則可能引起非常難以捕捉的bug, 折磨你我, 何不提前防范呢?
如果對迭代器失效問題一無所知, 則很容易寫出如下的錯誤代碼: #include <iostream> #include <map> using namespace std;typedef map<int, int> Map; typedef map<int, int>::iterator MapIt;void print(Map &m) {MapIt it;for(it = m.begin(); it != m.end(); it++){cout << it->second << " ";}cout << endl; }void deleteValueFromMap(Map &m, int n = 5) {MapIt it;for(it = m.begin(); it != m.end(); it++){if(0 == it->second % n){m.erase(it);}} }int main() {Map m;int i = 0;for(i = 0; i < 21; i++){m[i] = i;}print(m);deleteValueFromMap(m); // 程序崩潰return 0; }
運行上述程序, 結果程序崩潰, 什么原因呢? 原來, 對于關聯的容器map來說, m.erase(it);后, it就失效了, 而for循環中有it++, 自然而然會出問題啊。 那怎么辦呢? 且看: #include <iostream> #include <map> using namespace std;typedef map<int, int> Map; typedef map<int, int>::iterator MapIt;void print(Map &m) {MapIt it;for(it = m.begin(); it != m.end(); it++){cout << it->second << " ";}cout << endl; }void deleteValueFromMap(Map &m, int n = 5) {MapIt it;MapIt tmp;for(it = m.begin(); it != m.end(); /*不能再自增了*/){if(0 == it->second % n){tmp = it++;m.erase(tmp);}else{it++;}} }int main() {Map m;int i = 0;for(i = 0; i < 21; i++){m[i] = i;}print(m);deleteValueFromMap(m); // 程序okprint(m);return 0; }
結果為:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
當然, 上述程序也可以繼續簡化為: #include <iostream> #include <map> using namespace std;typedef map<int, int> Map; typedef map<int, int>::iterator MapIt;void print(Map &m) {MapIt it;for(it = m.begin(); it != m.end(); it++){cout << it->second << " ";}cout << endl; }void deleteValueFromMap(Map &m, int n = 5) {MapIt it;for(it = m.begin(); it != m.end(); /*不能再自增了*/){if(0 == it->second % n){m.erase(it++);}else{it++;}} }int main() {Map m;int i = 0;for(i = 0; i < 21; i++){m[i] = i;}print(m);deleteValueFromMap(m); // 程序okprint(m);return 0; }
結果ok.
有的朋友肯定會問, m.erase(it++);就不會產生迭代器失效么? 確實不會! 為什么呢? 這樣從it++說起, 為了簡便起見, 我們用p++來代替吧。 看程序: #include <iostream> using namespace std;int main() {char szTest[] = "abcdefg";char *p = szTest;cout << *p++ << endl;return 0; }
大家都知道, 結果為a. 但是, 很多人錯誤地以為是先執行*p, 然后執行p++, 其實, 這是個天大的誤解。 大家可以查一下*和++的執行順序, 雖然*和++的優先級相同, 但此處采取的是右結合方式, 實際上先執行的是p++, 不過, p++的返回值是原來的p, 也就是說, *p++的值還是a.
所以, 在m.erase(it++);中,it++先執行, 此時還沒有erase, 程序自然不會崩潰. 當it++執行完后, 已經指向了下一個元素了, 但it++的返回值還是當前元素, 此時再刪除它, 合情合理。
OK, map的迭代器失效問題, 我們先介紹到這里。
那vector又是怎樣的現象呢? 看程序: #include <iostream> #include <vector> using namespace std;typedef vector<int> Vec; typedef vector<int>::iterator VecIt;void print(Vec &v) {VecIt it;for(it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl; }void deleteValueFromVector(Vec &v, int n = 5) {VecIt it;for(it = v.begin(); it != v.end(); it++){if(0 == *it % n){v.erase(it);}} }int main() {Vec v;int i = 0;for(i = 0; i < 21; i++){v.push_back(i); // 不能再傻傻地用下標了}print(v);deleteValueFromVector(v); // 程序崩潰print(v);return 0; }
運行程序, 結果程序也崩潰, 可見, vector也存在迭代器失效的問題, 怎么改呢? 如下: #include <iostream> #include <vector> using namespace std;typedef vector<int> Vec; typedef vector<int>::iterator VecIt;void print(Vec &v) {VecIt it;for(it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl; }void deleteValueFromVector(Vec &v, int n = 5) {VecIt it;for(it = v.begin(); it != v.end(); /*不能再自增了*/){if(0 == *it % n){v.erase(it); // vector元素自動向前挪動了(關聯的map容器元素不會自動挪動), 所以不能再把it進行++了}else{it++;}} }int main() {Vec v;int i = 0;for(i = 0; i < 21; i++){v.push_back(i); // 不能再傻傻地用下標了}print(v);deleteValueFromVector(v); // 程序okprint(v);return 0; }
結果為:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
可見, vector迭代器失效的問題也不容忽視。
總之, 下次說到迭代器失效(尤其涉及到容器大小的改變, 比如刪除, 插入)時, 一定要留個心眼。 在實際編程中, 如果稍不注意, 則可能引起非常難以捕捉的bug, 折磨你我, 何不提前防范呢?
總結
以上是生活随笔為你收集整理的map和vector的迭代器失效问题(某公司招聘笔试试题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结构体作为STL map的key时需要注
- 下一篇: String 的普通构造函数、拷贝构造函