将enum ObjectType { UNKNOWN = 0, UNKNOWN_MOVABLE = 1, UNKNOWN_UNMOVABLE = 2, PEDESTRIAN = 3, BICYCLE = 4, VEHICLE = 5, MAX_OBJECT_TYPE = 6, };制作为二维表并读取x,y
时间: 2024-01-16 21:04:13 浏览: 73
EnumTools_v1_3_Ninject_dependency_enumtools_
可以将ObjectType看作是一维表的列名,然后将每个对象类型作为一维表的行,构成一个二维表。可以使用如下代码实现:
```c++
#include <iostream>
#include <string>
enum ObjectType {
UNKNOWN = 0,
UNKNOWN_MOVABLE = 1,
UNKNOWN_UNMOVABLE = 2,
PEDESTRIAN = 3,
BICYCLE = 4,
VEHICLE = 5,
MAX_OBJECT_TYPE = 6,
};
std::string ObjectTypeToString(ObjectType type) {
switch (type) {
case UNKNOWN:
return "UNKNOWN";
case UNKNOWN_MOVABLE:
return "UNKNOWN_MOVABLE";
case UNKNOWN_UNMOVABLE:
return "UNKNOWN_UNMOVABLE";
case PEDESTRIAN:
return "PEDESTRIAN";
case BICYCLE:
return "BICYCLE";
case VEHICLE:
return "VEHICLE";
default:
return "UNKNOWN";
}
}
int main() {
// 构建二维表
const int ROWS = ObjectType::MAX_OBJECT_TYPE;
const int COLS = 2;
std::string table[ROWS][COLS];
for (int i = 0; i < ROWS; ++i) {
ObjectType type = static_cast<ObjectType>(i);
table[i][0] = ObjectTypeToString(type);
table[i][1] = std::to_string(type);
}
// 读取二维表的值
int x, y;
std::cout << "请输入x和y的值:" << std::endl;
std::cin >> x >> y;
if (x >= 0 && x < ROWS && y >= 0 && y < COLS) {
std::cout << "table[" << x << "][" << y << "] = " << table[x][y] << std::endl;
} else {
std::cout << "输入的x或y值不合法!" << std::endl;
}
return 0;
}
```
以上代码中,`table`数组是二维表,其中每一行表示一个对象类型,第一列是对象类型的字符串表示,第二列是对象类型的整数值。
`ObjectTypeToString`函数是将对象类型转换为字符串的辅助函数。
在`main`函数中,首先构建二维表,然后读取用户输入的x和y值,如果输入的值合法,则输出对应的二维表中的值。
阅读全文