从字符串转换时,如何组合多个枚举
How can I combine multiple enums when converting from a string?
受代码评审中这个问题的启发,我正在学习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::map
或std::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的类型集,看看它是否包含该原始类型。这也很灵活,因为如果添加了三种或更多类型的口袋妖怪,它将支持它们。
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 访问在 C++ 结构中声明的枚举变量
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- typedef 枚举和枚举类有什么区别?
- 为什么我的开关/机箱在使用枚举时默认?
- 标准::可选枚举的比较运算符
- 尝试在枚举值中组合标志
- 三重限制的正整数组合的非递归枚举
- 枚举价值的组合爆炸(729组合..)
- 从字符串转换时,如何组合多个枚举
- 函数采用枚举或枚举值组合的模式
- 组合枚举 c++
- 调用枚举<<运算符进行字符串组合
- 最佳实践是使用枚举使用按位参数组合