在迭代期间调用集合上的 erase()
Calling erase() on set during iteration
我有以下代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<string> S;
S.insert("item1");
S.insert("item2");
S.insert("item3");
S.insert("item4");
int i=0;
for (set<string>::iterator it = S.begin(); it != S.end(); it++)
{
string temp = *it;
if (i++%2)
{
S.erase(temp); // Causes Seg Fault on next iteration
}
}
cout<<"Items Removed\n";
return 0;
}
上面的代码试图根据一个简单的条件从集合中删除元素。当使用 mingw-w64 (gcc 7.1.0 x86_64-posix-seh-rev0) 编译并出现分段错误时,它在我的系统上失败。
现在我假设这是因为 erase()
使当前元素的迭代器无效,从而导致 it++
失败。但我很困惑为什么这在我尝试过的所有在线 IDE 上都能正常工作 (Repl.it, IdeOne, CodeChef, Coilru, Cpp.sh)。想法?
如您所见,std::set::erase will invalidate the iterators to the erased element. Then the code leads to undefined behavior,这意味着一切皆有可能,但没有任何保证;虽然它看起来很好用,但你根本不能依赖它。
我有以下代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<string> S;
S.insert("item1");
S.insert("item2");
S.insert("item3");
S.insert("item4");
int i=0;
for (set<string>::iterator it = S.begin(); it != S.end(); it++)
{
string temp = *it;
if (i++%2)
{
S.erase(temp); // Causes Seg Fault on next iteration
}
}
cout<<"Items Removed\n";
return 0;
}
上面的代码试图根据一个简单的条件从集合中删除元素。当使用 mingw-w64 (gcc 7.1.0 x86_64-posix-seh-rev0) 编译并出现分段错误时,它在我的系统上失败。
现在我假设这是因为 erase()
使当前元素的迭代器无效,从而导致 it++
失败。但我很困惑为什么这在我尝试过的所有在线 IDE 上都能正常工作 (Repl.it, IdeOne, CodeChef, Coilru, Cpp.sh)。想法?
如您所见,std::set::erase will invalidate the iterators to the erased element. Then the code leads to undefined behavior,这意味着一切皆有可能,但没有任何保证;虽然它看起来很好用,但你根本不能依赖它。