将2D点阵列转换为1D阵列

时间:2014-01-04 12:44:57

标签: c++ arrays multidimensional-array mapping

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

2D点阵列的尺寸为 steps×number_of_robots 。因此,1D缓冲区具有 长度为步数×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缓冲区时它应该是相同的。我从文件中读取了一行点,因此它们应该完全相同。

这些点来自文件读取的输入。如何做到这一点似乎并不重要。事实是, main.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

的main.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;
}

1 个答案:

答案 0 :(得分: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
        }
    }
}