mister
mister

Reputation: 3433

STL Map doesn't erase

I have the following method in my program.

Weird thing is the data is not removed after I call erase.

Any idea?

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;
    }
}

Output:

  ERROR

Thanks! :)

Upvotes: 0

Views: 1075

Answers (1)

juanchopanza
juanchopanza

Reputation: 227448

This block:

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

prints out ERROR if an element with key id is not found in the map. Hence it has been removed.

Upvotes: 4

Related Questions