在unordered_map中找到一个枚举并获取其值?

时间:2013-10-04 20:14:24

标签: c++

给出以下代码:

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.
}

1 个答案:

答案 0 :(得分:4)

KeyState value = keys[key];
return value == KeyState::Up || value == KeyState::Down;

当然,这些是您为KeyState定义的唯一两个值,因此,它就是......

return true;