C++ Pointer to unordered_map entry changes, but entry not -
i'm trying set variable of unordered_map entry "chunk" calling function using pointer entry. pointer changes value "chunkindex", map entry not
glm::ivec3 chunkindex(1, 1, 1); chunks.insert(make_pair(chunkindex, chunk())); chunk = &chunks[chunkindex]; chunk->setchunkindex(chunkindex); logvector(chunk->chunkindex); // output: 1, 1, 1 logvector(chunks[chunk->chunkindex].chunkindex); // output: 0, 0, 0
"chunks" unordered_map of type:
typedef unordered_map<glm::ivec3, chunk, keyhash, keyequal> chunkmap;
do know why pointer changes value, , not referenced object?
thanks in advance!
update:
chunks.insert(make_pair(chunkindex, chunk())); log((chunks.find(chunkindex) == chunks.end()) ? "true" : "false");
this code outputs true, inserted entry doesn't exist!
this might useful too:
struct keyhash { size_t operator()(const glm::ivec3& k)const { return std::hash<int>()(k.x) ^ std::hash<int>()(k.y) ^ std::hash<int>()(k.z); } }; struct keyequal { bool operator()(const glm::ivec3& a, const glm::ivec3& b)const { return a.x < b.x || (a.x == b.x && a.y < b.y) || (a.x == b.x && a.y == b.y && a.z < b.z); } }; typedef unordered_map<glm::ivec3, chunk, keyhash, keyequal> chunkmap;
iterating through keys outputs 1, 1, 1
for (auto : chunks) { logvector(it.first); }
your keyequal
doesn't implement equality. replace with:
return a.x == b.x && a.y == b.y && a.z == b.z;
Comments
Post a Comment