C++取决于输入的矩阵大小无法编译

发布于 2024-09-28 07:42:13 字数 689 浏览 3 评论 0原文

有一个关于这个主题的主题正在处理数组,但我无法让它处理矩阵。

(主题:C++ 数组大小依赖于函数参数导致编译错误

总之:

  long rows, columns;
  cin >> rows >> columns;
  char *a = new char [rows];    

在 Visual Studio 中编译得很好,但是:

  char **a = new char[rows][columns]; 

  char *a[] = new char[rows][columns];

  char *a[] = new char[rows][columns]();

  char **a = new char[rows][columns]();

根本不编译。

有什么帮助吗?谢谢

there is a topic about this subject which is working with arrays but I can't make it work with matrices.

(Topic: C++ array size dependent on function parameter causes compile errors)

In summary:

  long rows, columns;
  cin >> rows >> columns;
  char *a = new char [rows];    

compiles great in visual studio, but:

  char **a = new char[rows][columns]; 

or

  char *a[] = new char[rows][columns];

or

  char *a[] = new char[rows][columns]();

or

  char **a = new char[rows][columns]();

don't compile at all.

Any help? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除会话 2024-10-05 07:42:13

array-new 运算符仅分配一维数组。有不同的解决方案,具体取决于您想要哪种数组结构。对于仅在运行时已知的维度,您可以创建一个数组数组:

char **a = new char*[rows];
for (int i = 0; i < rows; ++i) {
    a[i] = new char[columns];
}

或一个元素数组,其中包含指向每行第一个元素的关联指针数组(只需要对内存分配器进行两次命中):

char *a = new char[rows*columns];
char **a = new char*[rows];
for (int i = 0; i < rows; ++i) {
    a[i] = a + i*columns;
}

任一者都可以让您通过a[row][colum]访问矩阵元素。

另一种解决方案是仅使用一维数组并手动生成索引:

char *a = new char[rows*columns];
...
a[columns*row + column]

这可能比前两个解决方案中所需的双重间接更快。

您当然可以将其包装在一个类中以保留二维索引语法的外观:

class MatrixWrapper {
    ...
    char& operator()(int row, int column) { return a_[columns*row + column]; }
    ...
};
...
a(row, column)

The array-new operator only allocates 1-dimensional arrays. There are different solutions, depending on what sort of array structure you want. For dimensions only known at runtime, you can either create an array of arrays:

char **a = new char*[rows];
for (int i = 0; i < rows; ++i) {
    a[i] = new char[columns];
}

or an array of elements with an associated array of pointers to the first element of each row (requiring just two hits to the memory allocator):

char *a = new char[rows*columns];
char **a = new char*[rows];
for (int i = 0; i < rows; ++i) {
    a[i] = a + i*columns;
}

Either one will let you access matrix elements via a[row][column].

An alternate solution is to just use the one-dimensional array and generate indexes by hand:

char *a = new char[rows*columns];
...
a[columns*row + column]

This is probably faster than the double-indirection required in the first two solutions.

You could of course wrap this in a class to preserve a semblance of 2D indexing syntax:

class MatrixWrapper {
    ...
    char& operator()(int row, int column) { return a_[columns*row + column]; }
    ...
};
...
a(row, column)
农村范ル 2024-10-05 07:42:13

n 维数组不能像您尝试的那样直接分配。这是一种方法。

int main(){
    unsigned int **p;
    unsigned int rows = 10;
    unsigned int cols = 20;

    p = new unsigned int* [rows];

    for(size_t row = 0; row < rows; row++){
        p[row] = new unsigned int [cols];
    }

    for(size_t row = 0; row < rows; row++){
        delete [] p[row];
    }

    delete [] p;
}

A n-dimensional array can not be directly allocated as you are trying to. Here is a way to do it.

int main(){
    unsigned int **p;
    unsigned int rows = 10;
    unsigned int cols = 20;

    p = new unsigned int* [rows];

    for(size_t row = 0; row < rows; row++){
        p[row] = new unsigned int [cols];
    }

    for(size_t row = 0; row < rows; row++){
        delete [] p[row];
    }

    delete [] p;
}
捶死心动 2024-10-05 07:42:13

如果你打算在 c++ 中使用 c 数组(而不是 std::vector),你就会被迫做这样的事情:

long rows    = 0;
long columns = 0;
cin >> rows >> columns;

// declaration
long** a = new long* [rows];
for(long i = 0; i < rows; ++i)
{
    a[i] = new long[cols];
}

// initialization
for(long j = 0; j < rows; ++j)
{
    for(long i = 0; i < rows; i++)
    {
        a[i][j] = 0;
    }
}

// delete sub-arrays
for(long i = 0; i < rows; ++i)
{
    delete[] a[i];
}

// delete array
delete[] a;

If you're going to use c-arrays in c++ (instead of std::vector), you're stuck doing something like this:

long rows    = 0;
long columns = 0;
cin >> rows >> columns;

// declaration
long** a = new long* [rows];
for(long i = 0; i < rows; ++i)
{
    a[i] = new long[cols];
}

// initialization
for(long j = 0; j < rows; ++j)
{
    for(long i = 0; i < rows; i++)
    {
        a[i][j] = 0;
    }
}

// delete sub-arrays
for(long i = 0; i < rows; ++i)
{
    delete[] a[i];
}

// delete array
delete[] a;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文