C++多维数组

C++ Multidimensional Array

本文关键字:数组 C++      更新时间:2023-10-16

我正试图找出如何用这种方式填充多维数组:输入:行=3,列=3:

1 4 7
2 5 8
3 6 9

有人能给我一个主意吗?

p.S我的任务是找出在这两种安排中有多少核心保持在同一位置。例如:

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

所以处于相同位置的数字是:1 5 9。我试过了:

//n = 3 , m = 3
for(int i = 0; i <n; i++) {
for(int j = 0; j <m; j++){
if(array[i][j] == array2[i][j]) {
lol++;
}
}
}
cout<<lol;

/*
1 2 3
4 5 6
7 8 9

1 4 7
2 5 8
3 8 9
*/

它必须显示3,但它显示0,问题在哪里?

初始化时填充:

int a[3][3] = { { 1, 4, 7},
                { 2, 5, 8},
                { 3, 6, 9}
              };

编辑(不确定是否已解决):

在更新到问题之后,这里有一个示例应用程序,它(经过修改以接受用户的输入)将区分两个数组,并构建一个数组,指示相同的元素和相同元素的数量:

#include <iostream>
int** make_array(const size_t a_rows, const size_t a_columns)
{
    int** result = new int*[a_rows];
    for (size_t i = 0; i < a_rows; i++)
    {
        *(result + i) = new int[a_columns];
    }
    return result;
}
void print_array(int** a_array, const size_t a_rows, const size_t a_columns)
{
    for (size_t r = 0; r < a_rows; r++)
    {
        for (size_t c = 0; c < a_columns; c++)
        {
            std::cout << *(*(a_array + r) + c) << " ";
        }
        std::cout << "n";
    }
    std::cout << "n";
}
int main()
{
    // Example data.
    int a[3][3] = { { 1, 4, 7},
                    { 2, 5, 8},
                    { 3, 6, 9}
                  };
    int b[3][3] = { { 1, 2, 3},
                    { 4, 5, 6},
                    { 7, 8, 9}
                  };
    size_t rows    = 3;
    size_t columns = 3;
    // Create three arrays:
    //  - two input arrays
    //  - array that represents which elements are the same
    int** in_1 = make_array(rows, columns);
    int** in_2 = make_array(rows, columns);
    int** diff = make_array(rows, columns);
    // Populate with example data.
    for (size_t r = 0; r < rows; r++)
    {
        for (size_t c = 0; c < columns; c++)
        {
            *(*(in_1 + r) + c) = a[r][c];
            *(*(in_2 + r) + c) = b[r][c];
        }
    }
    // Diff.
    // The 'diff' array will hold '1' for elements that
    // were the same and '0' for elements that were not.
    size_t same_count  = 0;
    for (size_t r = 0; r < rows; r++)
    {
        for (size_t c = 0; c < columns; c++)
        {
            *(*(diff + r) + c) = *(*(in_1 + r) + c) == *(*(in_2 + r) + c);
            same_count += *(*(diff + r) + c);
        }
    }
    std::cout << "n";
    // Results.
    print_array(in_1, rows, columns);
    print_array(in_2, rows, columns);
    print_array(diff, rows, columns);
    std::cout << "Same element count: " << same_count << "n";
    // Free...
    return 0;
}

输出:

$ ./cpp/main.exe
1 4 7
2 5 8
3 6 9
1 2 3
4 5 6
7 8 9
1 0 0
0 1 0
0 0 1
Same element count: 3

如果只在运行时知道维度,则创建一个动态分配的数组:

int** x = new int*[rows];
for ( int i = 0 ; i < rows ; i++ )
   x[i] = new int[cols];

然后填充:

for ( int i = 0 ; i < rows ; i++ )
for ( int j = 0 ; i < cols ; j++ )
   x[i][j] = y;

或者更好的是,使用矢量中的矢量,这将给你更多的灵活性:

std::vector<std::vector<int> > x;

一个简单的解决方案是:-

int k = 1;
for(int i = 0; i < row; i++){
  for(int j = 0; j < col; j++){
    a[j][i] = k; // filling it in the column first order
    ++k;
  }
}