如何在 C++ 中定义具有 'n' 行和 'm' 列的多维数组并使用 For 循环迭代值?

How to define a multidimensional array in C++ with 'n' rows and 'm' columns and iterate values using For Loop?

我想要一个程序,询问多维数组的行数和列数,然后使用 For 循环迭代数组中的值。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, m, x;
    int a[n][m];
    cin>>n>>m;
    for(int i; i<n ; i++)
    {
        for(int j;  j<m ; j++)
        {
            cout<<"Enter the values";
            cin>>x;
            a[i][j] = x;
        }
    }
    return 0;
}

这里出现错误:

main.cpp|6|warning: 'm' is used uninitialized in this function [-Wuninitialized]|
main.cpp|6|warning: 'n' is used uninitialized in this function [-Wuninitialized]|

您不能声明数组大小未知。你必须动态地做。

#include <iostream>
using namespace std;

int main()
{
    int n = 0, m = 0;

    //. Get the matrix's size
    while (true)
    {
        cout << "Input the row count: "; cin >> n;
        cout << "Input the column count: "; cin >> m;

        if (n < 1 || m < 1)
        {
            cout << "Invalid values. Please retry." << endl;
            continue;
        }

        break;
    }

    //. Allocate multi-dimensional array dynamically.
    int ** mat = new int *[n];
    for (int i = 0; i < n; i++)
    {
        mat[i] = new int[m];
    }

    //. Receive the elements.
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << "Input the element of (" << i + 1 << "," << j + 1 << "): ";
            cin >> mat[i][j];
        }
    }

    //. Print matrix.
    cout << endl << "Your matrix:" << endl;
    for (int i = 0; i < n; i++)
    {       
        for (int j = 0; j < m; j++)
        {
            cout << mat[i][j] << "\t";
        }
        cout << std::endl;
    }

    //. Free memories.
    for (int i = 0; i < n; i++)
    {
        delete[] mat[i];
    }
    delete[] mat;

    return 0;
}

如果你喜欢用stl,可以很简单

#include <iostream>
#include <vector>
using namespace std;

using ROW = vector<int>;
using MATRIX = vector<ROW>;

int main()
{
    int n = 0, m = 0;
    MATRIX mat;

    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        ROW row;
        row.resize(m);

        for (int j = 0; j < m; j++)
        {
            cin >> row[j];
        }
        mat.push_back(row);
    }

    for (auto & row : mat)
    {
        for (auto & iter : row)
        {
            cout << iter << "\t";
        }
        cout << endl;
    }

    return 0;
}

一些评论。

  • 请不要使用#include<bits/stdc++.h>。这是一个 none C++ 兼容的编译器扩展
  • 请不要使用using namespace std;。始终使用完全限定名称。
  • 对于上述 to 语句,您会在 SO
  • 上找到数千个条目
  • 在 C++ 中,您不能使用 VLA,即可变长度数组,例如 int a[n][m];。这不是 C++ 语言的一部分
  • 您根本不应该使用 C 样式数组。使用 std::array 或根据您的情况 std::vector.
  • 使用有意义的变量名
  • 写评论
  • 在使用之前始终初始化所有变量!!!

最后但并非最不重要的一点。您不会在这个 none 意义上的“编程竞赛”网站上学习 C++。

数百万种可能的 C++ 解决方案(高级)之一可能如下所示:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    
    // Read the dimension of the 3d data
    if (unsigned int numberOfRows{}, numberOfCoulmns{}; (std::cin >> numberOfRows >> numberOfCoulmns) and (numberOfRows > 0u) and (numberOfCoulmns > 0u)) {

        // Define a vector with the requested size
        std::vector<std::vector<int>> data(numberOfRows, std::vector<int>(numberOfCoulmns, 0));

        // Read all data
        std::for_each(data.begin(), data.end(), [&](std::vector<int>& col) mutable
            { auto it = col.begin();  std::copy_n(std::istream_iterator<int>(std::cin), numberOfCoulmns, it++); });

        // Show debug output
        std::for_each(data.begin(), data.end(), [](std::vector<int>& col)
            {std::copy(col.begin(), col.end(), std::ostream_iterator<int>(std::cout, "\t")); std::cout << '\n'; });

    }
    else std::cerr << "\nError: Invalid input given\n\n";
    return 0;
}