c ++二维数组问题 [英] c++ two dimensional array question

查看:239
本文介绍了c ++二维数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个指向2维指针数组的指针(宽度和高度 x )。

这个代码会做我期望的吗? (创建数组元素,写出一些关于它们的信息,然后释放所有分配的内存。)

I would like to create something like a pointer to a 2 dimensional array of pointers (with the width and the height of x).
Will this code do what I expect? (Create array elements, writing out some information about them, then release all the allocated memory.)

int main(int argc, char** argv) {
    int x = 3;
    Node ***array = new Node**[x];
    for (int i = 0; i < 3; i++) {
        array[i] = new Node*[x];
        for (int j = 0; j < x; j++) {
            array[i][j] = new Node(i, j); /* Node::Node(int row, int col) */
        }
    }
    /* ....... */
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < x; j++) {
            cout << array[i][j]->row << ", " << array[i][j]->col << endl;
        }
    }
    /* ....... */
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < x; j++) {
            delete array[i][j];
            //array[i][j] = NULL;
        }
        delete[] array[i];
        //array[i] = NULL;
    }
    delete[] array;
    //array = NULL;
    return 0;
}

或者我应该创建一个向量向量向节点的指针?

否则应该分配我的对象在堆栈?

Or should I create a vector of vector of pointers to Node objects?
Or else should I allocate my objects on the stack?

(我使用指针,因为在Java或C#中,您必须始终使用 new 创建一个对象(然而,我不认为所有的对象都在堆内存中),并且我读到堆上有更多的可用空间。)

(I'm using pointers, because in Java or C#, you have to always use the new keyword when creating an object (however, I don't think all the objects are in the heap memory), and I read that there are more space available on the heap.)

另一个原因是我使用指针与 new 关键字,我想创建多个指针到同一个对象。
我应该在堆栈上创建一个对象,只是创建指向该对象的指针?

An other reason I use pointers with the new keyword, that I would like to create multiple pointers to the same object. Should I create one object on stack, and just create pointers to that object?

推荐答案

vector<向量<节点> > boost :: multi_array ,或者你可以汇总你自己的动态二维数组类(这不是那么难),它是一个平面1D std :: vector 的包装器。

I recommend that you use vector< vector<Node> >, boost::multi_array, or you can roll-up your own dynamic 2D array class (it's not that hard) that is a wrapper around a flat 1D std::vector.

所有上述解决方案都会将您的Node对象存储在堆中,并将负责清理内存。

All of the above solutions will store your Node objects in the heap, and will take care of cleaning up memory.

下面是一个简单的Matrix类的示例,它是 std :: vector< T> 的包装器:

Here's an example of a simple Matrix class that is a wrapper around std::vector<T>:

#include <iostream>
#include <vector>

template <class T>
class Matrix
{
public:
    Matrix() : width_(0), height_(0), vec_(0) {}

    Matrix(size_t width, size_t height)
        : width_(width), height_(height), vec_(width*height) {}

    size_t size() const {return vec_.size();}

    size_t width() const {return width_;}

    size_t height() const {return height_;}

    // Clears all preexisting data
    void resize(size_t width, size_t height)
        {width_ = 0; height_ = 0; vec_.clear(); vec_.resize(width*height);}

    void clear() {width_ = 0; height_ = 0; vec_.clear();}

    T& operator()(size_t col, size_t row) {return vec_[row*width_ + col];}

    const T& operator()(size_t col, size_t row) const
        {return vec_[row*width_ + col];}

private:
    size_t width_;
    size_t height_;
    std::vector<T> vec_;
};

int main()
{
    Matrix<double> a(3, 4);
    a(1, 2) = 3.1415;
    std::cout << a(1,2) << "\n";
}

它使用 operator()以模仿c风格多维数组的数组[2] [4] 语法。你不必担心浅拷贝,释放内存等,因为向量已经负责。

It uses operator() to mimic the array[2][4] syntax of c-style multidimensional arrays. You don't have to worry about shallow copies, freeing memory, etc, because vector already takes care of that.

这篇关于c ++二维数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆