【问题标题】:Convert 2D array of points into 1D array将点的二维数组转换为一维数组
【发布时间】:2014-01-22 03:46:05
【问题描述】:

我正在努力将二维点数组转换为一维整数数组。我写了一个包装类来为我做这件事(Array3D),它为我做映射填充底层缓冲区,但看起来索引完全错误,因为当我打印我的二维数组并比较缓冲区时,它给我不同的输出。

二维点数组的维度为 steps × number_of_robots。因此,一维缓冲区有 步长 × number_of_robots × 2

想法是这样的

buffer[index(x,y,0)] corresponds to points[index(x,y)].x
buffer[index(x,y,1)] corresponds to points[index(x,y)].y

输出错误,因为当我打印出 2D 点数组和 1D 缓冲区时,它应该是相同的。我从文件中读取了这一行点,因此它们应该完全一样。

这些点来自文件读取的输入。如何做到这一点似乎并不重要。事实上,ma​​in.cpp 的输出是:

(0, 4)  (0, 5)  (1, 5)  (2, 5)  (2, 4)  (3, 4)  (2, 4)  (2, 3)  (2, 2)  
(4, 0)  (4, -1) (4, 0)  (4, 1)  (3, 1)  (4, 1)  (4, 2)  (3, 2)  (2, 2)  

(0, 2)  (0, 3)  (1, 2)  (2, 2)  (2, 2)  (3, 3)  (2, 2)  (2, 2)  (2, 2)  
(1, 2)  (2, 2)  (2, 2)  (3, 3)  (2, 2)  (2, 2)  (2, 2)  (3, 3)  (2, 2)  

point.cpp

Point::Point(int a, int b) {
    x = a;
    y = b;
}

Array3D.cpp

template<class T>
int Array3D<T>::index(int x,int y,  int z) {
    return (x * ydim + y) * zdim + z;
}

template<class T>
T Array3D<T>::get( int x,  int y, int z) {
    return buffer[index(x,y,z)];
}

template<class T>
void Array3D<T>::set( int x,  int y, int z ,T n) {
    buffer[index(x,y,z)] = n;
}

Harvester.cpp

int Harvester::index(int t, int n) {
    return t*number_of_robots + n;
}

void Harvester::extract(Array3D<int> *array) {
    Point p;
    for(int t = 0; t < steps; t++ ) {
        for(int n = 0; n < number_of_robots; n++) {
            p = data[index(t,n)];
            array->set(t,n,0,p.x);
            array->set(t,n,1,p.x);
        }
    }
}

void Harvester::read_points(string filename) {
    string line;
    ifstream input;

    input.open(filename.c_str());

    input >> number_of_robots;

    int x, y;
    for(int n = 0; n < number_of_robots; n++) {
        if(input >> x >> y) {
            data[index(0,n)].x = x;
            data[index(0,n)].y = y;
            //cout << x << " " << y << endl;
        } else {
            cout << "Your file is bad, and you should feel bad!";
            return;
        }
    }
}

void Harvester::print_harvest() {
    for (int n = 0; n < number_of_robots; n++) {
        for (int t = 0; t < steps; t++) {
            data[index(t,n)].dump();
        }
        cout << endl;
    }
    cout << endl;
}

robots_002.txt

2
0 4
4 0

ma​​in.cpp

int main() {
    int mission_time;
    int number_of_robots;
    Point goal;
    string path;
    bool print = true;

    int choice = 2;    

    mission_time = 8;
    number_of_robots = 2;
    goal.x = 2;
    goal.y = 2;
    path = "robots_002.txt"; 

    int steps = mission_time + 1;

    Harvester h(mission_time, number_of_robots, goal);
    h.read_points("fixtures/" + path);
    h.run();


    int *buffer = new int[steps * number_of_robots * 2];
    Array3D<int> arr(steps, number_of_robots, 2, buffer);

    h.extract(&arr);

    h.print_harvest();
    for (int n = 0; n < number_of_robots; n++) {
        for (int t = 0; t < steps; t++) {
                printf("(%d, %d)\t", arr.get(t, n, 0), arr.get(t, n, 1));
        }
        cout << endl;
    }


    return 0;
}

【问题讨论】:

  • 所以输出是错误的,但是你没有显示输入,甚至没有解释输出是怎么错的。祝你好运......
  • 我认为很明显,当我想映射某些东西时,两者的输出应该是相同的。我对此添加了更多解释。

标签: c++ arrays multidimensional-array mapping


【解决方案1】:

仍在查看但快速观察。在 Harverster::extract 中,您将两者都设置为 p.x

void Harvester::extract(Array3D<int> *array) {
    Point p;
    for(int t = 0; t < steps; t++ ) {
        for(int n = 0; n < number_of_robots; n++) {
            p = data[index(t,n)];
            array->set(t,n,0,p.x);
            array->set(t,n,1,p.x);  //<-- im thinking you want this to be p.y
        }
    }
}

【讨论】:

  • 您还确定您的 Array3D 索引方法将唯一索引返回到您的一维数组中(如果不知道 ydim 和 zdim 是什么,我无法完全评估它)。但在我看来,由于上述错误(Harvester::extract() 将两个值都设置为 p.x),你的一维数组中的每个点都应该有 x 和 y 彼此相等,但它们不是根据你的输出。让我相信 Array3d::index() 的返回值对于每个唯一输入都没有唯一的输出。
  • 我在谷歌上搜索了如何将 1D 数组索引到 3D 数组,这看起来不对。但是非常感谢您发现我的愚蠢错误。我用stackoverflow.com/questions/7367770/…
  • stackoverflow.com/questions/3902648/… + 你的改变提示给了我想要的输出。
猜你喜欢
  • 2016-02-18
  • 2018-02-19
  • 2020-03-26
  • 2021-11-23
  • 2021-07-20
  • 2021-11-21
相关资源
最近更新 更多