c++如何遍历删除map/vector里面的元素

对于c++里面的容器, 我们可以使用iterator进行方便的遍历. 但是当我们通过iterator对vector/map等进行修改时, 我们就要小心了, 因为操作往往会导致iterator失效, 之后的行为都变得不可预知. 比如:

 1 #include <iostream>
 2 #include <vector>
 3  
 4 using namespace std;
 5  
 6 int main()
 7 {
 8     vector<int> a = {12, 23, 34, 45, 56, 67, 78, 89};
 9      
10     for (auto iter = a.begin(); iter != a.end(); ++iter) {
11         if (*iter > 30) {
12             a.erase(iter);
13         }
14     }
15      
16     for (const auto &element : a) {
17         cout<<element<<endl;
18     }
19      
20     return 0;
21 }
22  
23 输出:
24  
25 12
26 23
27 45
28 67
29 89

cplusplus的reference里对 std::vector::erase 的描述是:

Iterators, pointers and references pointing to position (or first) and beyond are invalidated, with all iterators, pointers and references to elements before position (or first) are guaranteed to keep referring to the same elements they were referring to before the call.

只有删除元素前面的iterator还保持有效, 之后的遍历行为不可预知.

 

解决方案

对于vector, erase会返回下一个iterator, 因此我们可以使用如下的方法:

 1 #include <iostream>
 2 #include <vector>
 3  
 4 using namespace std;
 5  
 6 int main()
 7 {
 8     vector<int> a = {12, 23, 34, 45, 56, 67, 78, 89};
 9      
10     auto iter = a.begin();
11     while (iter != a.end()) {
12         if (*iter > 30) {
13             iter = a.erase(iter);
14         }
15         else {
16             ++iter;
17         }
18     }
19      
20     for (const auto &element : a) {
21         cout<<element<<endl;
22     }
23      
24     return 0;
25 }
26  
27  
28 输出:
29  
30 12
31 23

对于map, 删除iterator只会影响当前的iterator, 因此使用for循环就够了, 比如:

 1 #include <iostream>
 2 #include <map>
 3  
 4 using namespace std;
 5  
 6 int main()
 7 {
 8     map<int, int> a = {{1, 12}, {2, 23}, {3, 34}, {4, 45}, {5, 56}, {6, 67}};
 9      
10     for (auto iter = a.begin(); iter != a.end(); ++iter) {
11         if (iter->second > 30) {
12             a.erase(iter);
13         }
14     }
15      
16     for (const auto &element : a) {
17         cout<<element.first<<" : "<<element.second<<endl;
18     }
19      
20     return 0;
21 }
22  
23 输出:
24  
25 1 : 12
26 2 : 23

但是更推荐的做法是在erase前让iterator指向下一个元素

 1 #include <iostream>
 2 #include <map>
 3  
 4 using namespace std;
 5  
 6 int main()
 7 {
 8     map<int, int> a = {{1, 12}, {2, 23}, {3, 34}, {4, 45}, {5, 56}, {6, 67}};
 9      
10     auto iter = a.begin();
11     while (iter != a.end()) {
12         if (iter->second > 30) {
13             a.erase(iter++);
14         }
15         else {
16             ++iter;
17         }
18     }
19      
20     for (const auto &element : a) {
21         cout<<element.first<<" : "<<element.second<<endl;
22     }
23      
24     return 0;
25 }
26  
27 输出:
28  
29 1 : 12
30 2 : 23

 

 

摘自:https://www.cnblogs.com/dabaopku/p/3912662.html

 

原文链接: https://www.cnblogs.com/luoluosha/p/12921923.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    c++如何遍历删除map/vector里面的元素

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/349660

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午5:49
下一篇 2023年3月2日 上午5:49

相关推荐