根据输入动态二维数组

2021-12-18 00:00:00 matrix multidimensional-array vector c++

我需要从用户那里得到一个输入 N 并生成一个 N*N 矩阵.我如何声明矩阵?一般来说,数组和矩阵的大小应该在声明时固定,对吗?vector> 怎么样?我以前从未使用过这个,所以我需要资深人士的建议.

I need to get an input N from the user and generate a N*N matrix. How can I declare the matrix? Generally, the size of the array and matrix should be fixed at the declaration, right? What about vector<vector<int>> ? I never use this before so I need suggestion from veteran.

推荐答案

Boost 在其 uBLAS 库,并提供如下使用语法.

Boost implements matrices (supporting mathematical operations) in its uBLAS library, and provides usage syntax like the following.

#include <boost/numeric/ublas/matrix.hpp>

int main(int argc, char* argv[])
{
    unsigned int N = atoi(argv[1]);
    boost::matrix<int> myMatrix(N, N);

    for (unsigned i = 0; i < myMatrix.size1 (); ++i)
        for (unsigned j = 0; j < myMatrix.size2 (); ++j)
            myMatrix(i, j) = 3 * i + j;

    return 0;
}

相关文章