迭代时从 std::set 中删除元素

编程入门 行业动态 更新时间:2024-10-17 00:22:26
本文介绍了迭代时从 std::set 中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要检查一组并删除符合预定义条件的元素.

I need to go through a set and remove elements that meet a predefined criteria.

这是我写的测试代码:

#include <set> #include <algorithm> void printElement(int value) { std::cout << value << " "; } int main() { int initNum[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::set<int> numbers(initNum, initNum + 10); // print '0 1 2 3 4 5 6 7 8 9' std::for_each(numbers.begin(), numbers.end(), printElement); std::set<int>::iterator it = numbers.begin(); // iterate through the set and erase all even numbers for (; it != numbers.end(); ++it) { int n = *it; if (n % 2 == 0) { // wouldn't invalidate the iterator? numbers.erase(it); } } // print '1 3 5 7 9' std::for_each(numbers.begin(), numbers.end(), printElement); return 0; }

起初,我认为在迭代时从集合中删除一个元素会使迭代器无效,并且 for 循环中的增量会产生未定义的行为.尽管如此,我执行了这段测试代码并且一切顺利,但我无法解释为什么.

At first, I thought that erasing an element from the set while iterating through it would invalidate the iterator, and the increment at the for loop would have undefined behavior. Even though, I executed this test code and all went well, and I can't explain why.

我的问题:这是标准集的定义行为还是此实现特定?顺便说一句,我在 ubuntu 10.04(32 位版本)上使用 gcc 4.3.3.

My question: Is this the defined behavior for std sets or is this implementation specific? I am using gcc 4.3.3 on ubuntu 10.04 (32-bit version), by the way.

谢谢!

建议的解决方案:

这是从集合中迭代和擦除元素的正确方法吗?

Is this a correct way to iterate and erase elements from the set?

while(it != numbers.end()) { int n = *it; if (n % 2 == 0) { // post-increment operator returns a copy, then increment numbers.erase(it++); } else { // pre-increment operator increments, then return ++it; } }

首选解决方案

我想出了一个对我来说似乎更优雅的解决方案,即使它完全一样.

I came around a solution that seems more elegant to me, even though it does exactly the same.

while(it != numbers.end()) { // copy the current iterator then increment it std::set<int>::iterator current = it++; int n = *current; if (n % 2 == 0) { // don't invalidate iterator it, because it is already // pointing to the next element numbers.erase(current); } }

如果 while 内有多个测试条件,则每个条件都必须递增迭代器.我更喜欢这段代码,因为迭代器只在一个地方递增,使代码不易出错且更具可读性.

If there are several test conditions inside the while, each one of them must increment the iterator. I like this code better because the iterator is incremented only in one place, making the code less error-prone and more readable.

推荐答案

这取决于实现:

标准 23.1.2.8:

Standard 23.1.2.8:

插入成员不会影响迭代器的有效性和对容器的引用,而擦除成员只会使迭代器和对被擦除元素的引用无效.

The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.

也许你可以试试这个——这是符合标准的:

Maybe you could try this -- this is standard conforming:

for (auto it = numbers.begin(); it != numbers.end(); ) { if (*it % 2 == 0) { numbers.erase(it++); } else { ++it; } }

注意it++是后缀,因此它通过旧位置擦除,但由于运算符的原因首先跳转到较新的位置.

Note that it++ is postfix, hence it passes the old position to erase, but first jumps to a newer one due to the operator.

2015.10.27 更新:C++11 已经解决了这个缺陷.iterator erase (const_iterator position); 将迭代器返回到最后一个被删除元素之后的元素(或 set::end,如果最后一个元素被删除).所以C++11的风格是:

2015.10.27 update: C++11 has resolved the defect. iterator erase (const_iterator position); return an iterator to the element that follows the last element removed (or set::end, if the last element was removed). So C++11 style is:

for (auto it = numbers.begin(); it != numbers.end(); ) { if (*it % 2 == 0) { it = numbers.erase(it); } else { ++it; } }

更多推荐

迭代时从 std::set 中删除元素

本文发布于:2023-11-29 21:27:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   迭代   std   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!