【问题标题】:Mapping an integer to an array将整数映射到数组
【发布时间】:2011-11-20 15:17:29
【问题描述】:

我最近开始接触 C++;我是一个业余程序员,我懂一点 Python。

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

我决定将蛇可以采取的可能方向放在一个枚举中:

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);

并为所需函数映射枚举:

void fill_map(std::map<directions,V4> &map_vec){
    map_vec[UP] = V4(0,1,0,0);
    map_vec[DOWN] = V4(0,-1,0,0);
    //others

  }

 void fill_map(std::map<directions, directions> &map){
    map[UP]= DOWN;
    map[DOWN]= UP;
    //others
  }


void fill_map_axis(std::map<int, directions*> &map_dir){
    directions array_x[2] = {RIGHT,LEFT};
    map_dir[0] = array_x;

    directions array_y[2] = {UP,DOWN};//store the array
    map_dir[1] = array_y;

    directions array_z[2] = {FW,RW};//store the array
    map_dir[2] = array_z;

    directions array_w[2] = {IN,OUT};//store the array
    map_dir[3] = array_w;

}

在蛇构造函数中调用 fill_map 函数。
基本上我想要在 fill_map_axis 中做的是映射一个与坐标索引相对应的整数(0 coord x,1 coord y 等)并映射沿这些轴移动的两个方向。 所以我存储了一个包含两个方向的数组。

现在我调用函数:

directions SnakeCPU::find_dir(V4 point){
    //point is the target point
    directions dir;
    int index = get_coord_index(point); //get the index where to move
    double diff = head_pos[index]-point[index]; //find the difference between the head and the target point

    directions* axis = dir_coords[index]; //call the map containing the directions stored in an array.

    if(diff<0.){

        dir = *axis; //use the first
    }
    else if(diff>0.) {
        axis++;
        dir = *axis; //use the second
    }
    else{
        dir = NONE;
    }

    return dir;
}

虽然在Snake构造函数中初始化了map,但是从指针轴返回的值似乎是一个随机内存块。

所以我的问题是:你看到代码中有错误吗?我是否使用了有感觉的指针轴?

我真的不是指针专家;在 Python 中,地图是用这样的字典实例化的:

dir_coords = {0:[LEFT,RIGHT], ...}

所以我只需要调用它:

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...
}

【问题讨论】:

  • 您的array_x 等是局部变量,您只是插入指向这些数组的指针。函数作用域退出后,这些指针不再有效。简而言之,一切都在上升。我建议在您的地图中存储std::pair&lt;direction, direction&gt;
  • SnakeCPU构造函数的代码在哪里?
  • head_pos[] and point[] 是什么?此外,axis++; dir = *axis; 可以替换为 dir = '*(++axis);
  • @Kerrek,回答一下。
  • 我想,很好 :D 什么是 std::pair?我把它叫做pair[0]和pair[1]?

标签: c++ pointers map


【解决方案1】:

只是一个意外,这就是我的设计方式。

#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_vecmap_dir 全局常量,因为我了解到它们本质上就是这样。为了初始化它们,我依赖于新的 C++11 初始化语法。如果这不是一个选项,我们也可以用传统的方式填充地图:

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)));
// ...

(是的,你也可以写map_dir[0] = DirectionPair(RIGHT, LEFT)'。不过我不喜欢方括号,我觉得它们太暴力了。)

【讨论】:

  • 我喜欢这种形式 map["key"]=value is like python ;) 再次感谢您的帮助!
  • @Pella86:没问题。如果您对这些结构有任何问题,请告诉我 :-) 方括号是非常量的,如果缺少元素会导致元素被插入,所以我不愿意使用它们,但我们当然希望在这里 创建元素,所以我无缘无故过度恐慌。
  • 很高兴知道我没有想到可能的缺点!但无论如何问题都解决了(现在 cpu 蛇比我快……该死的,我每一轮都输了……)
【解决方案2】:

正如 Kerrek 所说,您使用 自动 范围变量填充了 map_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_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

【讨论】:

  • 最后的分配似乎没有意义:array_x 是一个数组,而不是 directions
  • 是的。我一直认为它是一个指向单个方向的指针,并且部分编码为这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多