简体   繁体   English

stl hash_map-修改键

[英]stl hash_map - modifying key

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? 有副作用吗?

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). 没有C ++关联容器支持以显着方式更改键( 显着意味着更改会更改散列容器中的哈希结果或有序容器中的比较结果)。

If you did modify the key (by circumventing the const correctness system in some way) you'd get unpredictable results from lookups. 如果您确实修改了密钥(通过某种方式避开了const正确性系统),则查找将导致无法预测的结果。

Not only can you not change the key since it's a const member of the pair , you can't erase or insert members into the hash_map without invalidating the iterator, i , you have. 因为它是该pairconst成员,所以您不仅不能更改密钥,而且还必须在没有使迭代器i无效的情况下将其擦除或插入hash_map When i is invalidated, you can't increment it to get the next item from the container. i无效时,您无法对其进行递增以从容器中获取下一项。

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. 可能(可能是)有一个更好的算法,但是我认为您需要做的是将要更改其键的元素的元素(或键)的副本存储在其他临时容器中在您的for循环中。 Then walk the temportary container and use the information in it to: 然后走临时容器,并使用其中的信息来:

  • get the element you want to change the key for in the original hash_map container 获取要更改原始hash_map容器中的键的元素
  • erase() that element from the original container 从原始容器中erase()该元素
  • insert() a new element with the new key and original value back into the hash_map insert()将具有新键和原始值的新元素放回hash_map

Then you can dump the temporary container. 然后,您可以转储临时容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM