STL Map不会删除

时间:2012-05-16 05:51:29

标签: c++ map erase

我的程序中有以下方法。

奇怪的是,在我调用erase之后,数据没有被删除。

有什么想法吗?

map<int,obj>::iterator it = this->indexMap.find(id);
if(it != this->indexMap.end())
{
    int mapSize = this->indexMap.size();
    int dataSize = (*it).second.getDataMap().size();

    //copy data to another node | even when it doesn't get into this if condition, it does not remove the data
    if(mapSize> 1 && dataSize != 0)
    {
        it++;
        this->copyData(id,it->first);
        it--;
    }

    //remove peer | i've tried id and it, both does not work
    this->indexMap.erase(it);

    map<int,obj>::iterator iter = this->indexMap.find(id);
    if(iter == this->indexMap.end())
    {
        cout << "ERROR" << endl;
    }
}

输出:

  ERROR

谢谢! :)

1 个答案:

答案 0 :(得分:4)

这个区块:

map<int,obj>::iterator iter = this->indexMap.find(id);
if(iter == this->indexMap.end())
{
    cout << "ERROR" << endl;
}
如果在地图中找不到具有键ERROR的元素,则

打印出id。因此它已被删除。

相关问题