从字符串转换时,如何组合多个枚举

How can I combine multiple enums when converting from a string?

本文关键字:组合 枚举 何组合 转换 字符串      更新时间:2023-10-16

受代码评审中这个问题的启发,我正在学习C++的一些基础知识,部分是通过编写Pokedex。我目前正在定义枚举中的口袋妖怪类型:

enum class basicPokemonType
{
Normal,
Fire,
Water,    
//etc.
};

在《代码评论》上接受的答案中,作者建议将这些内容组合成一个组合类,我如下所述(这里有评论,以确保我理解自己在做什么):

class combinedPokemonType
{
combinedPokemonType(basicPokemonType primary);
combinedPokemonType(basicPokemonType primary, basicPokemonType secondary); //overloading constructor in case we have two types
combinedPokemonType convertStringToPokemonType(std::string primary, std::string secondary = "")
};

在代码中,我将每个口袋妖怪从一个文本文件中读取到一个流中,如下所示:

1,Bulbasaur,Grass and Poison,15.2,28,雄性和雌性,0,

如您所见,口袋妖怪可以有多种类型。我当前将字符串转换为我定义的枚举的函数如下:

combinedPokemonType combinedPokemonType::convertStringToPokemonType(std::string primary, std::string secondary="")
{
if (primary == "Normal")
{
return combinedPokemonType(basicPokemonType::Normal);
}
else if (primary == "Fire")
{
return combinedPokemonType(basicPokemonType::Fire);
}
else if (primary == "Water")
{
return combinedPokemonType(basicPokemonType::Water);
}
// etc.
}

我如何涵盖有两种以上类型的情况?我是否需要继续我的if语句,并定义它们之间的每一个可能组合?我忍不住觉得一定有更简单的方法。

或者,如果我错过了一些明显的东西,或者尝试了一些明显超出我目前能力的东西,请随时告诉我。

我通常的建议是将其实现为std::mapstd::unordered_map:

std::map<std::string, basicPokemonType> const types_map = {
{"Fire", basicPokemonType::Fire},
{"Normal", basicPokemonType::Normal},
{"Water", basicPokemonType::Water},
/*...*/
};
combinedPokemonType combinedPokemonType::convertStringToPokemonType(std::string primary, std::string secondary="")
{
auto it = types_map.find(primary);
if(it != types_map.end())
return combinedPokemonType(*it);
else
//Whatever your error condition is if the string isn't a valid type
}

这并不能防止您必须写出每个单独的对,但它确实使代码更干净,并减少了重复的if/else if/else语句。

您可以将pokemon的类型实现为具有一组原始类型。要检查pokemon是否是特定类型,您可以询问pokemon的类型集,看看它是否包含该原始类型。这也很灵活,因为如果添加了三种或更多类型的口袋妖怪,它将支持它们。