给出以下代码:
enum Keys {
Up,
Down,
Left,
Right
};
enum KeyState {
Down,
Up
};
std::unordered_map<Keys, KeyState> keys;
keys[Keys::Up] = KeyState::Up;
keys[Keys::Down] = KeyState::Up;
keys[Keys::Left] = KeyState::Down;
keys[Keys::Right] = KeyState::Up;
我如何编写这样的方法:
isKeyDown(Keys key) {
return true if key has the Up or Down state.
}
答案 0 :(得分:4)
KeyState value = keys[key];
return value == KeyState::Up || value == KeyState::Down;
当然,这些是您为KeyState
定义的唯一两个值,因此,它就是......
return true;