次のように定義されたハッシュマップがあります
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();
}
私の質問は、必要に応じてキーを変更する正しい方法でしょうか? 悪い副作用はありますか?