내 프로그램에 다음과 같은 방법이 있습니다.STL지도가 지워지지 않습니다
내가 지우는 호출 후에 데이터가 삭제되지 않는 이상한 점은.
아이디어가 있으십니까?
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 :: find()'가 컨테이너에서 요소를 찾을 수없는 경우, 마지막 요소 즉,'map :: end()'을 지나서 하나의 요소로 반복자를 반환한다. 'ERROR'입니다. find()가 반환하는 것과 혼동을 느낍니까? Q에 게시 된 코드와 출력에서 요소가 실제로 제거 된 것 같습니다. –
그것이 제거되지 않았다는 것이 무엇을 의미합니까? 나는 그것이 그 것처럼 보인다. – juanchopanza
'it ++'이'indexMap.end()'를 주면 어떻게 될까? 이것을 역 참조하면 UB가 생깁니다. –