我有一个包含字符串值的二维数组。我在迭代时使用枚举来跟踪数组的第一个索引。我试图找到给定字符串匹配的枚举(数组的第一维)。现在,我迭代数组和检查值的方式似乎并不是 100% 有效,因为它会返回错误的枚举。有谁看到我做错了什么,或者知道基于第二个匹配字符串获取第一个数组索引的更好方法?
注意:我使用的是 Arduino,并且使用的是字符串对象而不是 char*。
enum conditionType {
CLEAR = 0,
OVERCAST,
CLOUDY,
RAIN,
THUNDERSTORM,
SNOW
};
int conditionsIndex[6] = {
CLEAR, OVERCAST, CLOUDY, RAIN, THUNDERSTORM, SNOW};
const char *conditions[][20] = {
// CLEAR
{
"Clear" }
,
// OVERCAST
{
"Partly Cloudy" }
,
// CLOUDY
{
"Shallow Fog",
"Partial Fog",
"Mostly Cloudy",
"Fog","Overcast",
"Scattered Clouds" }
,
// RAIN
{
"Drizzle",
"Rain",
"Hail",
"Mist",
"Freezing Drizzle",
"Patches of Fog",
"Rain Mist",
"Rain Showers",
"Unknown Precipitation",
"Unknown",
"Low Drifting Widespread Dust",
"Low Drifting Sand" }
,
// THUNDERSTORM
{
"Thunderstorm",
"Thunderstorms and Rain",
"Thunderstorms and Snow",
"Thunderstorms and Ice Pellets",
"Thunderstorms with Hail",
"Thunderstorms with Small Hail",
"Blowing Widespread Dust",
"Blowing Sand",
"Small Hail",
"Squalls",
"Funnel Cloud" }
,
// SNOW
{
"Volcanic Ash",
"Widespread Dust",
"Sand",
"Haze",
"Spray",
"Dust Whirls",
"Sandstorm",
"Freezing Rain",
"Freezing Fog",
"Blowing Snow",
"Snow Showers",
"Snow Blowing Snow Mist",
"Ice Pellet Showers",
"Hail Showers",
"Small Hail Showers",
"Snow",
"Snow Grains",
"Low Drifting Snow",
"Ice Crystals",
"Ice Pellets" }
};
int currentCondition;
for ( int i = 0; i < ( sizeof(conditionsIndex) / sizeof(int) ); i++ ) {
int idx = conditionsIndex[i];
for (int j = 0; j < ( sizeof(conditions[idx]) / sizeof(String) ); j++ ) {
if ( forecast.equals(conditions[idx][j]) ) {
currentCondition = idx;
}
}
}