stl hash_map - 修改键

发布于 2024-08-22 02:22:14 字数 938 浏览 4 评论 0原文

我有一个哈希映射定义为

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2024-08-29 02:22:14

您必须从容器中删除该元素并使用新密钥重新添加它。

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.

祁梦 2024-08-29 02:22:14

您不仅不能更改密钥,因为它是 pairconst 成员,而且您也无法在没有更改的情况下将成员删除或插入到 hash_map 中。使迭代器 i 无效。当 i 无效时,您无法增加它以从容器中获取下一个项目。

可能有(而且可能是)更好的算法,但我认为您需要做的是将您想要更改键的元素的元素(或只是键)的副本存储在其他临时容器中在你的 for 循环中。然后遍历临时容器并使用其中的信息来:

  • 获取要在原始 hash_map 容器中更改其键的元素
  • erase() 从原始容器中删除该元素
  • insert() 将带有新键和原始值的新元素放回 hash_map

然后您可以转储临时容器。

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. When i 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:

  • get the element you want to change the key for in the original hash_map container
  • erase() that element from the original container
  • insert() a new element with the new key and original value back into the hash_map

Then you can dump the temporary container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文