Initialize and accessing 2D matrix pointer C++: 初始化和访问2D矩阵指针C++:

huangapple go评论85阅读模式
英文:

Initialize and accessing 2D matrix pointer C++

问题

我是新手学习C++,尝试掌握“指针思维”以提高编码效率。

我正试图编写著名的生命游戏(Game of Life)。因此,我需要一个二维矩阵。由于它将在每个纪元中进行修改(根据GoL规则),我希望将这个矩阵声明为指针,以便每次一个细胞进化时都能指向它(并防止重写)。

我理解我需要声明一个vectorvector<int>。我按照以下方式进行:

int i_size(50);
int j_size(50);
vector<int> arr(j_size, 0);
vector<vector<int>> *matrix;
matrix = new vector<vector<int>>;
    
for (int i(0); i<i_size; i++)
{
    matrix->push_back(arr);
}

在这里,我希望获得一个指向良好分配并初始化为0vector的指针matrix

到目前为止,我卡住了。

我知道如何打印我的矩阵的内存地址(cout << matrix)。
我知道如何打印矩阵中包含的每个向量的内存地址(cout << &matrix[i]

就是这样。

问题

  • 我的矩阵声明/初始化是否正确?
  • 如何访问我的矩阵?
  • 如何修改矩阵中的一个细胞?

感谢大家的帮助。再次强调,我是初学者,任何建议都将有所帮助! Initialize and accessing 2D matrix pointer C++:
初始化和访问2D矩阵指针C++:

英文:

I'm new in C++ and I try to get the "pointer mindset" to code with efficiency.

I'm trying to code el famoso Game of Life. So, I need a 2D matrix. Since it will be modify at each epoch (regarding the GoL rules), I would like to declare this matrix as a pointer to be able to point on it each time a cell evolves (and prevents rewriting).

I understood that I need to declare a vector of vector&lt;int&gt;. I proceed like this :

int i_size(50);
int j_size(50);
vector&lt;int&gt; arr(j_size, 0);
vector&lt;vector&lt;int&gt;&gt; *matrix;
matrix = new vector&lt;vector&lt;int&gt;&gt;;
    
for (int i(0); i&lt;i_size; i++)
{
    matrix-&gt;push_back(arr);
}

Here, I hope to get a pointer matrix which point to a vector of vector well allocated and initialized at 0.

At this point, I'm stuck.

I know how to print the memory address of my matrix (cout &lt;&lt; matrix).
I know how to print the memory address of each vector contained in matrix (cout &lt;&lt; &amp;matrix[i])

And that's all.

Questions

  • Is my matrix declaration/initialization correct ?
  • How can I access to my matrix ?
  • How can I modify one cell of my matrix ?

Thanx all for your help. Again, I'm beginner so any advice will be helpful ! Initialize and accessing 2D matrix pointer C++:
初始化和访问2D矩阵指针C++:

答案1

得分: 4

你的代码是正确的,但过于复杂了。动态分配向量并没有带来任何好处,因为std::vector已经自动管理其元素的动态分配。

一旦你决定使用嵌套向量(*),你的代码的简化版本如下:

int i_size(50);
int j_size(50);
std::vector<std::vector<int>> matrix(i_size, std::vector<int>(j_size));

使用更多的指针并不意味着性能更好,但大多数情况下会导致更多的错误和更复杂的代码。没有一种“指针思维方式”会导致效率。

(*)也许能够提高性能的方法是使用带有索引变换的一维向量。向量的一个重要优点是内存局部性,但由于向量动态分配一个数组来存储元素,嵌套向量中的内部元素不是连续的。或者考虑使用std::array(不会出现嵌套问题)。当你关心性能时,你必须进行性能分析和测量。

英文:

Your code is correct, but unecessarily complicated. You gain nothing by dynamically allocating the vector, because std::vector does already manage its elements dynamically.

Once you are settled with using a nested vector (*), the simple version of your code is this:

int i_size(50);
int j_size(50);
std::vector&lt;std::vector&lt;int&gt;&gt; matrix{ std::vector&lt;int&gt;(j_size),i_size};

Using more pointers does not imply more performance, but most of the time it implies more bugs and more complicated code. There is no "pointer mindset" that would lead to efficiency.

(*) What probably would gain you performance would be to use a flat 1D vector with index transformations. The big plus of a vector is its memory locality, but as the vector dynamically allocates an array to store the elements, the inner elements are not contigous in a nested vector. Alternatively consider to use std::array (which has no issue with nesting). When you do care about performance you must profile and measure.

答案2

得分: 0

首先,你的实现是正确的,因为创建了正确大小的矩阵,并且可以使用以下语法访问矩阵的值

(*matrix)[i][j]

同样,你也可以通过以下方式修改矩阵的每个元素的值

(*matrix)[i][j]=value;

然而,在这种情况下,指针的使用并不会增加程序的效率。Vector 已经负责动态内存分配和释放。
顺便说一下,有一种更高效的使用 vector 声明矩阵的方法是

vector<vector<int>> matrix(i_size, vector<int>(j_size, 0));

我认为指针更适合用于数组,例如,如果你想这样编写你的代码。

int** matrix = new int*[i_size];
for (int i = 0; i < i_size; i++) {
    matrix[i] = new int[j_size]{};
}

最后,如果你的目标是编写生命游戏,我想指出每个方格只有两种状态,所以尽量使用 bool 而不是 int。

英文:

First of all, your implementation is correct, as a matrix is created with the right size and you can access the value of the matrix with this syntax

(*matrix)[i][j]

In the same way you can modify the value of each element of the matrix by

(*matrix)[i][j]=value;

However, the use of a pointer doesn't increase the efficiency of your program in this case. Vector already take care of the dynamic memory allocation and deallocation.
By the way there is a more efficient way to declare the matrix with vector which would be

vector&lt;vector&lt;int&gt;&gt; matrix(i_size, vector&lt;int&gt;(j_size, 0));

I believe pointers are better use for arrays, so for exemple if you were to write your code like this.

int** matrix = new int*[i_size];
for (int i = 0; i &lt; i_size; i++) {
    matrix[i] = new int[j_size]{};
}

Finally, if your goal is to code the game of life , I would like to point out that there is only two state for each sqaure so try to use bool instead of int

huangapple
  • 本文由 发表于 2023年6月12日 17:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定