我正在尝试在 vector 中查找枚举项,似乎是一些 == 运算符问题。有什么指导吗?
这是枚举
enum RESPONSE{
GAME_START='P',
GAME_HELP='H',
GAME_QUIT='Q'
}
这是用法
std::string s = getinputChar(); //this returns a string with a one character.
std::vector<RESPONSE> responces;
responces.push_back(GAME_START);
responces.push_back(GAME_QUIT);
std::vector<RESPONSE>::iterator it = find (responces.begin(), responces.end(), s.c_str());
错误:
error C2678: binary '==': no operator found which takes a left-hand operand of type 'RESPONSE'(或者没有可接受的转换)
最佳答案
您可以接受他们的输入为 char
而不是 string
char s = getinputChar();
然后你可以把你的find
改成
std::vector<RESPONSE>::iterator it = find (responces.begin(), responces.end(), static_cast<RESPONSE>(s));
关于c++ - 在枚举 vector 中查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486122/