简体   繁体   English

在c ++中使用枚举作为数组索引

[英]using enum for an array index in c++

#include <stdlib.h>
#include <stdio.h>
using namespace std;



void main(){
    char *resolutions[] = { "720x480", "1024x600", "1280x720", "1920x1080" };

    int x = 0;

    enum ResMode
    {
        p480,
        p600,
        p720,
        p1080
    }; 
    ResMode res = p480;

    printf("\nPlease enter the resolution you wish to use now by entering a number");
    printf("\n480p[0], 600p[1], 720p[2], 1080p[3]");
    gets(res);

    printf("\nThe resolution you have selected is %s", resolutions[res]);

}

so basically i want to be able to press 1 and have it select p600 from enum and out put it as 1024x600 in the next line. 所以基本上我想能够按1并让它从枚举中选择p600并将其作为1024x600放在下一行。 I am getting a type conversion error. 我收到类型转换错误。 How can i fix this? 我怎样才能解决这个问题?

Looks like you want to associate some items with other items. 看起来您想要将某些项目与其他项目相关联 Usually associations are described in lookup tables or maps. 通常在查找表或映射中描述关联。

std::map<ResMode, std::string> map_table =
{
  {p480,     string("720x480")},
  {p600,     string("1024x600")},
  {p720,     string("1280x720")},
  {p1080,    string("1920x1080")},
};

int main(void)
{
  cout << map_table[p480] << "\n";
  return EXIT_SUCCESS;
}

Likewise, you can map menu selections to enums. 同样,您可以菜单选择映射到枚举。

Edit 1 编辑1

std::map<unsigned int, ResMode> selection_map =
{
  {0, p480}, {1, p600}, {2, p720}, {3, p1080},
};

int main(void)
{
  cout << "\n"
       << "Please enter the resolution you wish to use now by entering a number\n"
       <<"480p[0], 600p[1], 720p[2], 1080p[3]";
  unsigned int selection = 0;
  cin >> selection;
  if (selection < 4)
  {
    Resmode resolution_index = selection_map[selection];
    cout << "You chose: "
         << map_table[resolution_index]
         << "\n";
  }
  return EXIT_SUCCESS;
}

int 's are not implicitly convertible to an enum . int不能隐式转换为enum You will have to read in an int and then cast it yourself. 你必须读入一个int然后自己投射。 Example, 例,

int resInt;
scanf("%d", &resInt);
res = static_cast<ResMode>(resInt);//Note that this does not do bound checking.

You can use "scanf" instead of "gets", something like this: 您可以使用“scanf”而不是“gets”,如下所示:

scanf("%d",&res); // I recommend use scanf_s

Or the iostream library with std::cin. 或者带有std :: cin的iostream库。 But after taking the input, always, check if the input is the correct one. 但是在获取输入后,请务必检查输入是否正确。

As otehrs pointed out, there is no direct way of doing this. 正如otehrs指出的那样,没有直接的方法可以做到这一点。 However, there are some recipes/tricks that you can use. 但是,您可以使用一些配方/技巧。 I modified your code as follows: 我修改了你的代码如下:

#include <stdlib.h>
#include <stdio.h>


#define SOME_ENUM(DO) \
    DO(_720x480)  \
    DO(_1024x600) \
    DO(_1280x720) \
    DO(_1920x1080)

#define MAKE_ENUM(VAR) VAR,
enum class RESOLUTIONS
{
    SOME_ENUM(MAKE_ENUM)
};

#define MAKE_STRINGS(VAR) #VAR,
const char* const
RESOLUTION_NAMES[] =
{
    SOME_ENUM(MAKE_STRINGS)
};



const char *
GET_RESOLUTION_NAME(RESOLUTIONS type)
{
    return RESOLUTION_NAMES[static_cast<int>(type)];
}

int
GET_RESOLUTION_VALUE(RESOLUTIONS type)
{
    return static_cast<int>(type);
}

RESOLUTIONS
GET_RESOLUTION(int i)
{
    return static_cast<RESOLUTIONS>(i);
}



using namespace std;



int main(){


    printf("\nPlease enter the resolution you wish to use now by entering a number");

    printf("\n480p[0], 600p[1], 720p[2], 1080p[3]");

    int res_type;

    cin >> res_type;


    RESOLUTIONS selected_res = GET_RESOLUTION(res_type);

    printf("\nThe resolution you have selected is %s\n\n", GET_RESOLUTION_NAME(selected_res));

    return 0;

}

Sorry for not providing an explanation, as I have to go now. 很抱歉没有提供解释,因为我现在必须去。 This recipe can be found here . 这个食谱可以在这里找到。 The code works and compiles for c++11. 该代码适用于c ++ 11。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM