std::map<> 的自定义键
Custom Key for std::map<>
我正在尝试使用以下 struct
作为 std::map
的自定义密钥:
struct ActionId {
// ENCAPSULATED MEMBERS
private:
size_t _id;
static size_t _root;
static size_t incrementedRoot() {
return (_root += 1);
}
// INTERFACE
public:
ActionId() :
_id(incrementedRoot()) { }
ActionId(const ActionId& that) :
_id(that._id) { }
ActionId& operator=(const ActionId& that) {
this->_id = that._id;
return *this;
}
bool operator==(const ActionId& that) const {
return this->_id == that._id;
}
bool operator!=(const ActionId& that) const {
return this->_id != that._id;
}
bool operator<(const ActionId& that) const {
return this->_id < that._id;
}
};
以下字典是单独 InputManager
class 的成员:
std::map<ActionId, std::set<sf::Keyboard::Key>> _keyBindings;
在这个成员函数中访问的是:
std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const {
return _keyBindings[action];
}
不幸的是,该函数抛出此编译器错误:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<Game2D::ActionId,std::set<sf::Keyboard::Key,std::less<_Kty>,std::allocator<_Kty>>,std::less<Game2D::ActionId>,std::allocator<std::pair<const Game2D::ActionId,_Ty>>>
' (or there is no acceptable conversion)
根据 this article, the operator<()
member of ActionId
with const
qualification should be sufficient to make it a custom map key, while this article 的说法,我需要的只是使 ActionId
可复制和可分配。显然,我的结构满足这两个条件,那么为什么 InputManager::keysBoundTo()
无法编译?
索引运算符(“[]”)是 std::map 的非常量成员函数。鉴于,您已明确指出 keysBoundTo
是常量成员。
将keysBoundTo
重写为
std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const
{
auto it = keyBindigs_.find(action);
if ( it == keyBindings_.end() )
return std::set<sf::Keyboard::Key>();
else
return it->second;
}
请注意,我已将您的成员变量重命名为具有尾随下划线。不要使用带有前导下划线的标识符。
我正在尝试使用以下 struct
作为 std::map
的自定义密钥:
struct ActionId {
// ENCAPSULATED MEMBERS
private:
size_t _id;
static size_t _root;
static size_t incrementedRoot() {
return (_root += 1);
}
// INTERFACE
public:
ActionId() :
_id(incrementedRoot()) { }
ActionId(const ActionId& that) :
_id(that._id) { }
ActionId& operator=(const ActionId& that) {
this->_id = that._id;
return *this;
}
bool operator==(const ActionId& that) const {
return this->_id == that._id;
}
bool operator!=(const ActionId& that) const {
return this->_id != that._id;
}
bool operator<(const ActionId& that) const {
return this->_id < that._id;
}
};
以下字典是单独 InputManager
class 的成员:
std::map<ActionId, std::set<sf::Keyboard::Key>> _keyBindings;
在这个成员函数中访问的是:
std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const {
return _keyBindings[action];
}
不幸的是,该函数抛出此编译器错误:
error C2678: binary '[' : no operator found which takes a left-hand operand of type '
const std::map<Game2D::ActionId,std::set<sf::Keyboard::Key,std::less<_Kty>,std::allocator<_Kty>>,std::less<Game2D::ActionId>,std::allocator<std::pair<const Game2D::ActionId,_Ty>>>
' (or there is no acceptable conversion)
根据 this article, the operator<()
member of ActionId
with const
qualification should be sufficient to make it a custom map key, while this article 的说法,我需要的只是使 ActionId
可复制和可分配。显然,我的结构满足这两个条件,那么为什么 InputManager::keysBoundTo()
无法编译?
索引运算符(“[]”)是 std::map 的非常量成员函数。鉴于,您已明确指出 keysBoundTo
是常量成员。
将keysBoundTo
重写为
std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const
{
auto it = keyBindigs_.find(action);
if ( it == keyBindings_.end() )
return std::set<sf::Keyboard::Key>();
else
return it->second;
}
请注意,我已将您的成员变量重命名为具有尾随下划线。不要使用带有前导下划线的标识符。