将整数映射到数组 我最近开始用C++语言编写;我是一个业余程序员,我懂一点Python

将整数映射到数组 我最近开始用C++语言编写;我是一个业余程序员,我懂一点Python,c++,pointers,map,C++,Pointers,Map,我给一条小蛇编了程序。我想插入另一条由计算机引导的蛇 我决定在枚举中指出蛇可能的方向: enum directions{UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW,NONE}; void fill_map(std::map<directions,V4> &map_vec); void fill_map(std::map<int, directions*> &map_dir); void fill_map(std::m

我给一条小蛇编了程序。我想插入另一条由计算机引导的蛇

我决定在枚举中指出蛇可能的方向:

enum directions{UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW,NONE};

void fill_map(std::map<directions,V4> &map_vec);

void fill_map(std::map<int, directions*> &map_dir);

void fill_map(std::map<directions,directions> &map);
所以我只需要称之为:

axis = dir_coords[index]
dir = axis[0]
#or
dir = axis[1]
编辑:

Snake构造函数:

Snake::Snake()
{    
    fill_map(dir_vectors);
    fill_map(dir_coords);
    fill_map(opposite_dir);
    head_pos = V4(0.,0.,0.,0.);
    //other stuff...
}

这是我设计这个的方式

#include <map>

enum EDirection { NONE = 0, UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW };

typedef std::map<EDirection, V4>           DirectionMap;
typedef std::pair<EDirection, EDirection> DirectionPair;
typedef std::map<int, DirectionPair>            PairMap;

extern const DirectionMap map_vec {
  { UP,   (0, 1, 0, 0) },
  { DOWN, (0,-1, 0, 0) },
  // ...
};   // using C++11 initialization lists for convenience

extern const PairMap map_dir {
  { 0, { RIGHT, LEFT } },
  { 1, { UP, DOWN }    },
  // ...
};

(是的,你也可以写
map\u dir[0]=DirectionPair(RIGHT,LEFT)
。虽然我不喜欢方括号,但它们对我来说太暴力了。)

正如Kerrek所说,你用自动作用域变量填充了map\u dir,这意味着函数结束时它们会自动销毁

void fill_map_axis(std::map<int, directions*> &map_dir){
    //This variable will be destroyed when the function ends
    directions array_x[2] = {RIGHT,LEFT};  
    map_dir[0] = array_x; //should have a warning at least, probably not compile
void fill\u map\u轴(标准::map和map\u dir){
//此变量将在函数结束时销毁
方向数组_x[2]={右,左};
map_dir[0]=array_x;//至少应该有一个警告,可能没有编译
你可能想要的是地图上的实际方向

void fill_map_axis(std::map<int, std::pair<directions, directions> > &map_dir){
    //This variable will be destroyed when the function ends
    std::pair<directions, directions> array_x = {RIGHT,LEFT};  
    map_dir[0] = array_x; //makes a copy
void fill\u map\u轴(标准::map和map\u dir){
//此变量将在函数结束时销毁
std::对数组_x={右,左};
map_dir[0]=array_x;//创建一个副本

Your
array\u x
等都是局部变量,您只需插入指向这些数组的指针。函数作用域退出后,这些指针不再有效。简而言之,一切都已就绪。我建议在地图中存储
std::pair
。SnakeCPU构造函数的代码在哪里?什么是
head\u pos[]点[]
?而且,
axis++;dir=*axis;
可以替换为
dir='*(+++axis);
@Kerrek,回答这个问题。我想,好的:D std::pair是什么?我把它叫做pair[0]和pair[1]?最后一个任务似乎没有意义:
array\ux
是一个数组,而不是
方向。是的。我一直认为它是指向单个方向的指针,并且部分编码是这样的。我喜欢表单映射[“key”]=value类似于python;)和thx再次寻求帮助!@Pella86:没问题。如果您对这些构造有任何问题,请告诉我:-)方括号是非常量的,如果缺少元素,会导致插入元素,因此我不愿意使用它们,但当然这里我们想要创建元素,所以我毫无理由地过度恐慌。继续我很想知道我没有考虑到可能的缺点!但不管怎样,问题已经解决了(现在cpu蛇比我快…该死的,我就像每一轮一样都失败了…)
#include <map>

enum EDirection { NONE = 0, UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW };

typedef std::map<EDirection, V4>           DirectionMap;
typedef std::pair<EDirection, EDirection> DirectionPair;
typedef std::map<int, DirectionPair>            PairMap;

extern const DirectionMap map_vec {
  { UP,   (0, 1, 0, 0) },
  { DOWN, (0,-1, 0, 0) },
  // ...
};   // using C++11 initialization lists for convenience

extern const PairMap map_dir {
  { 0, { RIGHT, LEFT } },
  { 1, { UP, DOWN }    },
  // ...
};
PairMap map_dir;
map_dir.insert(std::make_pair(0, DirectionPair(RIGHT, LEFT)));
map_dir.insert(std::make_pair(1, DirectionPair(UP, DOWN)));
// ...

DirectionMap map_vec;
map_vec.insert(std::make_pair(UP,   V4(0, 1, 0, 0)));
map_vec.insert(std::make_pair(DOWN, V4(0,-1, 0, 0)));
// ...
void fill_map_axis(std::map<int, directions*> &map_dir){
    //This variable will be destroyed when the function ends
    directions array_x[2] = {RIGHT,LEFT};  
    map_dir[0] = array_x; //should have a warning at least, probably not compile
void fill_map_axis(std::map<int, std::pair<directions, directions> > &map_dir){
    //This variable will be destroyed when the function ends
    std::pair<directions, directions> array_x = {RIGHT,LEFT};  
    map_dir[0] = array_x; //makes a copy