我得到运行时错误,我怀疑是因为这段代码,我想知道如何迭代一组并从中间擦除它。
for (auto it : a) {
if (freq[it.first] == 1 || freq[it.second] == 1)
a.erase(it);
}
我尝试了这个解决方案,我在另一个问题上找到了它,但它似乎仍然无效:
for (set<pair<int, int> >:: iterator it = a.begin(); it != a.end();)
{
cout << it->first << " " << it->second << endl;
if (freq[it->first] == 1 || freq[it->second] == 1)
{
removed = true;
a.erase(it);
}
else
{
++it;
}
}
答案 0 :(得分:0)
这解决了它:
for (set<pair<int, int> >:: iterator it = a.begin(); it != a.end();/* blank */)
{
cout << it->first << " " << it->second << endl;
if (freq[it->first] == 1 || freq[it->second] == 1)
{
removed = true;
it = a.erase(it);
}
else
{
++it;
}
}