stl hash_map - 修改键
我有一个哈希映射定义为
class KeyType {
int key;
mutable bool flag;
KeyType(int key) : key(key), flag(false) {}
void setFlag() const { flag = true; }
};
struct KeyType_hasher {
size_t operator()(const KeyType& s) const {
return static_cast<size_t> key;
}
};
struct KeyType_equal {
size_t operator()(const KeyType& s1, const KeyType& s2) const {
return s1.key == s2.key;
}
};
typedef hash_map<KeyType , ValueType, KeyType_hasher, KeyType_equal > KeyValueMap;
稍后在代码中我有一个地方必须循环遍历映射并将函数应用于我找到的每个值。根据函数的结果,我还必须修改迭代器上的键。
KeyValueMap theMap;
// theMap[key1] = value1;
// theMap[key2] = value2;
// theMap[key3] = value3;
for(KeyValueMap::iterator i = theMap.begin(); i != theMap.end(); ++i) {
if(true == ValueFunction(i->second))
i->first.setFlag();
}
我的问题是,如果必须的话,这是修改密钥的正确方法吗? 它有什么不良副作用吗?
I have a hash map defined as
class KeyType {
int key;
mutable bool flag;
KeyType(int key) : key(key), flag(false) {}
void setFlag() const { flag = true; }
};
struct KeyType_hasher {
size_t operator()(const KeyType& s) const {
return static_cast<size_t> key;
}
};
struct KeyType_equal {
size_t operator()(const KeyType& s1, const KeyType& s2) const {
return s1.key == s2.key;
}
};
typedef hash_map<KeyType , ValueType, KeyType_hasher, KeyType_equal > KeyValueMap;
Later on in the code I have a place where I have to loop though the map and apply a function to each value I find. Based on the function's outcome, I have to modify the key at the iterator as well.
KeyValueMap theMap;
// theMap[key1] = value1;
// theMap[key2] = value2;
// theMap[key3] = value3;
for(KeyValueMap::iterator i = theMap.begin(); i != theMap.end(); ++i) {
if(true == ValueFunction(i->second))
i->first.setFlag();
}
My question is, would that be the right way of modifying the key, if I have to?
Does it have any bad side effects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须从容器中删除该元素并使用新密钥重新添加它。
C++ 关联容器都不支持以显着方式更改密钥(其中“显着”意味着更改会更改散列容器中的散列结果或有序容器中的比较结果)。
如果您确实修改了密钥(通过以某种方式规避 const 正确性系统),您将从查找中获得不可预测的结果。
You'd have to remove the element from the container and re-add it with the new key.
None of the C++ associative containers support changing the key in a significant way (where significant means the change alters the results of the hash in a hashed container or the comparrsion in an ordered container).
If you did modify the key (by circumventing the const correctness system in some way) you'd get unpredictable results from lookups.
您不仅不能更改密钥,因为它是
pair
的const
成员,而且您也无法在没有更改的情况下将成员删除或插入到hash_map
中。使迭代器i
无效。当i
无效时,您无法增加它以从容器中获取下一个项目。可能有(而且可能是)更好的算法,但我认为您需要做的是将您想要更改键的元素的元素(或只是键)的副本存储在其他临时容器中在你的
for
循环中。然后遍历临时容器并使用其中的信息来:hash_map
容器中更改其键的元素erase()
从原始容器中删除该元素hash_map
然后您可以转储临时容器。
Not only can you not change the key since it's a
const
member of thepair
, you can't erase or insert members into thehash_map
without invalidating the iterator,i
, you have. Wheni
is invalidated, you can't increment it to get the next item from the container.There might be (and probably is) a better algorithm, but what I think you'll need to do is store copies of the elements (or just the keys) of the elements you want to have the keys changed for in some other temporary container in your
for
loop. Then walk the temportary container and use the information in it to:hash_map
containererase()
that element from the original containerinsert()
a new element with the new key and original value back into thehash_map
Then you can dump the temporary container.