나는 std::unordered_map
을 사용하고 있으며 동적으로 생성하는 객체에 포인터를 저장하는 데 new
을 사용하고 싶습니다.C++ std :: unordered_map
개체를 잘 만들 수 있으며 삽입 메서드가 성공합니다. 그러나 색인을 사용하여지도에서 특정 객체를 분석하려고하면 목록의 모든 항목이지도에서 가리키는 마지막 항목에 매핑됩니다. (삽입)에서 5
의 위치에 5
점은, 그러나 그것은 값이 9
것을 말한다
typedef std::unordered_map<__int64, Session*> HashDict;
HashDict dict;
void Sessions::Insert()
{
Session* newSession = new Session;
newSession->setId(sessionCounter++);
dict.insert(HashDict::value_type(newSession->Id(), newSession));
}
void Sessions::Find(__int64 id)
{
HashDict::iterator it = dict.find(id);
if(it != dict.end())
{
Session* s = it->second;
std::cout << "Search for: " << id << std::setw(3) <<
" found @ location: " << it->second << std::setw(3) <<
" with value: " << s->Id() << std::endl;
}
}
001D4F50 0
001D5038 1
001D50D0 2
001D5378 3
001D5410 4
001D54A8 5
001D5540 6
001D55D8 7
001D5670 8
001D4E50 9
Search for: 5 found @ location: 001D54A8 with value: 9
알 수 있습니다.
내가 뭘 잘못하고 있니?
'세션'의 정의를 게시 할 수 있습니까? –
@downvoters Google의 의장을 싫어하기 때문에 다운 투표하지 않기를 바랍니다! –
생성 후 ID가 변경 될 수 있습니까? – bdonlan