2012-05-16 4 views
0

내 프로그램에 다음과 같은 방법이 있습니다.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 

감사합니다! :)

+0

'map :: find()'가 컨테이너에서 요소를 찾을 수없는 경우, 마지막 요소 즉,'map :: end()'을 지나서 하나의 요소로 반복자를 반환한다. 'ERROR'입니다. find()가 반환하는 것과 혼동을 느낍니까? Q에 게시 된 코드와 출력에서 ​​요소가 실제로 제거 된 것 같습니다. –

+0

그것이 제거되지 않았다는 것이 무엇을 의미합니까? 나는 그것이 그 것처럼 보인다. – juanchopanza

+0

'it ++'이'indexMap.end()'를 주면 어떻게 될까? 이것을 역 참조하면 UB가 생깁니다. –

답변

4

이 블록 : 키 id이있는 요소는지도에서 ERROR

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

지문을 찾을 수없는 경우. 그러므로 그것은 제거되었습니다.

+0

감사합니다! 그 오류를 만들었습니다. 잠의 부족이되어야합니다. – mister

+1

@dupdupdup 문제 없음, 나는 그 느낌을 안다. 당신은 다른 사람들이 당신의 질문에 여전히 답이 없다고 생각하면서 시간을 낭비하지 않도록 답을 받아 들여야 할 수도 있습니다. – juanchopanza

+0

죄송합니다. 어제 답변을 수락했지만 대기 시간 문제로 인해 이륙 이유를 모르겠습니다. 어쨌든 고마워요 – mister

관련 문제