I get run-time error and I suspect it's because of this block of code, I'd like to know how to iterate over a set and erase from it in the middle.
for (auto it : a) {
if (freq[it.first] == 1 || freq[it.second] == 1)
a.erase(it);
}
I tried this solution which I found on another question but it still doesn't seem to work:
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;
}
}