为什么在迭代集合/地图时不能对其进行编辑?
例:

bool flag;
set <int> :: iterator aIterator;
set <int> :: iterator bIterator;
bIterator = B.begin();
for (bIterator=B.begin(); bIterator!=B.end(); ++bIterator)
{
    flag = true;
    aIterator = A.begin();
    for (aIterator=A.begin(); aIterator!=A.end(); ++aIterator)
    {
        if (*bIterator == *aIterator)// if an elemnet in B is found in A, delete that element from A and check the next element of B
        {
            A.erase(aIterator);
            flag = false;
            break;
        }

    }
    if (flag == true)
        A.insert(*bIterator); //if not found then add that element to A(which now represents the set of symmetric difference)
}


我该如何处理?我的意思是,如何在迭代时编辑集合?

最佳答案

做这个:
for (aIterator=A.begin(); aIterator!=A.end(); ++aIterator)

不是那个:
for (bIterator=A.begin(); bIterator!=A.end(); ++aIterator)

aIterator未初始化,因此无法递增。

其余的看起来还可以。

10-06 00:31