简体   繁体   English

c++ 中的简单二维数组矩阵

[英]simple 2D array matrix in c++

im absolut new to cpp, I tried to code a simple 2D array matrix.我绝对是 cpp 的新手,我尝试编写一个简单的二维数组矩阵。 Here is what I'm talking about:这就是我所说的:


#include <iostream>
#include <string>

using namespace std;


void printTable()
{

    int tabelle [10][10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int i, j;
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            printf("%d   ", tabelle[i][j]);
        }
        printf("\n");
    }
}


int main(int argc, char* argv[])
{
    printTable();


    return 0;
}

This is how it looks like:这是它的样子:

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

And this is how it should look like:这就是它的样子:

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
2 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0

Thx for any advise:D感谢任何建议:D

That's not how array assignments work in C++.这不是 C++ 中数组分配的工作方式。 Think of 2D arrays as a 1D array of 1D arrays.将二维 arrays 视为一维 arrays 的一维数组。

int tabelle[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {3, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {5, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {6, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {7, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {8, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {9, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {10, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
  • your code is working correctly, 2d array is set of one dimension array.您的代码工作正常,二维数组由一维数组组成。
  • here 2d array tabelle[10][10]'s first one dimension array tablelle[0] is initialized with elements.这里二维数组 tableelle[10][10] 的第一个一维数组 tablelle[0] 用元素初始化。
  • One more thing partially initialized array fills the rest of the element with另一件事是部分初始化的数组填充元素的 rest
    zero, that is why all other element are zero.零,这就是为什么所有其他元素都为零。

There are several different methods to initialize your 2D array, with the same result:有几种不同的方法可以初始化二维数组,结果相同:

    int tab[4][4] = {{1,2,3,4},{2},{3},{4}};
    int bat[4][4] = {1,2,3,4,2,0,0,0,3,0,0,0,4,0,0,0};

The thing to keep in mind is that this kind of array is automatically allocated on the stack by the compiler and is stored as a linear sequential array in memory (see the second method above).需要记住的是,这种数组是由编译器自动分配到栈上的,并作为线性顺序数组存储在 memory 中(参见上面的第二种方法)。

Also, for C++, the array you create in a function by automatic allocation in this manner has a scope limited to that function; Also, for C++, the array you create in a function by automatic allocation in this manner has a scope limited to that function; after the function call ends, the array is gone. function 调用结束后,数组消失了。 If you want to create an array in a function and pass it back to the main function for use, it has to be dynamically (heap) allocated;如果要在 function 中创建一个数组并将其传回给主 function 使用,它必须是动态(堆)分配的; you then pass a pointer to that array back to main, and then you have to manage that memory by hand (assuming you didn't use a smart pointer to make it).然后,您将指向该数组的指针传递回 main,然后您必须手动管理 memory(假设您没有使用智能指针来实现它)。

Note: You can also initiate your array as follows, for very large arrays:注意:对于非常大的 arrays,您还可以按如下方式启动阵列:

   myarray[100][100] = {};    //sets all elements to 0
   for (int i = 0; i < 100; i++) {
       myarray[0][i] = i + 1;
       myarray[i][0] = i + 1;
   }

Never do this:永远不要这样做:

    myarray[100][100];

As this creates an array full of junk values loaded with whatever random bits happen to have been previously stored in that memory location, and you might forget to overwrite them later.因为这会创建一个充满垃圾值的数组,其中加载了之前碰巧存储在该 memory 位置中的任何随机位,并且您以后可能会忘记覆盖它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM