STL 映射不会擦除

STL Map doesn't erase

本文关键字:擦除 映射 STL      更新时间:2023-10-16

我的程序中有以下方法。

奇怪的是,在我调用擦除后,数据不会被删除。

知道吗?

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

谢谢! :)

此块:

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

如果地图中找不到具有键id的元素,则打印出ERROR。因此,它已被删除。