二维数组值

发布于 2024-10-16 21:58:58 字数 836 浏览 10 评论 0 原文

我想声明一个二维数组并为其赋值,而不运行 for 循环。

我想我可以使用以下想法,

int array[5] = {1,2,3,4,5};

它也可以很好地初始化二维数组。但显然我的编译器不喜欢这样。

/*
 1   8  12  20  25
 5   9  13  24  26
*/

#include <iostream.h>

int main()
{
    int arr[2][5] = {0};   // This actually initializes everything to 0.
    arr [1] [] = {1,8,12,20,25}; // Line 11
    arr [2] [] = {5,9,13,24,26};
    return 0;
}

J:\CPP\Grid>bcc32.exe Grid.cpp

用于 Win32 的 Borland C++ 5.5.1 版权所有 (c) 1993, 2000 Borland

网格.cpp:

错误 E2188 Grid.cpp 11:函数 main() 中的表达式语法

错误 E2188 Grid.cpp 12:函数 main() 中的表达式语法

警告 W8004 Grid.cpp 14:“arr”被分配了一个从未在 funct 中使用的值 离子主()

* 编译中有 2 个错误 *

请帮助了解使用我的值集初始化 2d 数组的正确方法。

I wanted to declare a 2D array and assign values to it, without running a for loop.

I thought I could used the following idea

int array[5] = {1,2,3,4,5};

Which works fine to initialize the 2D array as well. But apparently my compiler doesn't like this.

/*
 1   8  12  20  25
 5   9  13  24  26
*/

#include <iostream.h>

int main()
{
    int arr[2][5] = {0};   // This actually initializes everything to 0.
    arr [1] [] = {1,8,12,20,25}; // Line 11
    arr [2] [] = {5,9,13,24,26};
    return 0;
}

J:\CPP\Grid>bcc32.exe Grid.cpp

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

Grid.cpp:

Error E2188 Grid.cpp 11: Expression syntax in function main()

Error E2188 Grid.cpp 12: Expression syntax in function main()

Warning W8004 Grid.cpp 14: 'arr' is assigned a value that is never used in funct
ion main()

* 2 errors in Compile *

Please help as to what is the right way to initialize the 2d array with my set of values.

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

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

发布评论

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

评论(5

眼泪淡了忧伤 2024-10-23 21:58:58

像这样:

int main()
{
    int arr[2][5] =
    {
        {1,8,12,20,25},
        {5,9,13,24,26}
    };
}

这应该包含在你的 C++ 教科书中:你使用的是哪一本?

不管怎样,最好考虑使用 std::vector 或一些现成的矩阵类,例如来自 Boost 的矩阵类。

Like this:

int main()
{
    int arr[2][5] =
    {
        {1,8,12,20,25},
        {5,9,13,24,26}
    };
}

This should be covered by your C++ textbook: which one are you using?

Anyway, better, consider using std::vector or some ready-made matrix class e.g. from Boost.

日暮斜阳 2024-10-23 21:58:58

在 C 或 C++ 中初始化多维数组的正确方法是

int arr[2][5] = {{1,8,12,20,25}, {5,9,13,24,26}};

如果需要,您可以使用相同的技巧来初始化更高维的数组。

另外,在初始代码中要小心 - 您试图在数组中使用 1 索引偏移量来初始化它。这没有编译,但如果编译了,就会导致问题,因为 C 数组是 0 索引的!

The proper way to initialize a multidimensional array in C or C++ is

int arr[2][5] = {{1,8,12,20,25}, {5,9,13,24,26}};

You can use this same trick to initialize even higher-dimensional arrays if you want.

Also, be careful in your initial code - you were trying to use 1-indexed offsets into the array to initialize it. This didn't compile, but if it did it would cause problems because C arrays are 0-indexed!

后来的我们 2024-10-23 21:58:58

只是想指出您不需要指定数组的所有维度。

最左边的维度可以由编译器“猜测”。

#include <stdio.h>
int main(void) {
  int arr[][5] = {{1,2,3,4,5}, {5,6,7,8,9}, {6,5,4,3,2}};
  printf("sizeof arr is %d bytes\n", (int)sizeof arr);
  printf("number of elements: %d\n", (int)(sizeof arr/sizeof arr[0]));
  return 0;
}

Just want to point out you do not need to specify all dimensions of the array.

The leftmost dimension can be 'guessed' by the compiler.

#include <stdio.h>
int main(void) {
  int arr[][5] = {{1,2,3,4,5}, {5,6,7,8,9}, {6,5,4,3,2}};
  printf("sizeof arr is %d bytes\n", (int)sizeof arr);
  printf("number of elements: %d\n", (int)(sizeof arr/sizeof arr[0]));
  return 0;
}
枕头说它不想醒 2024-10-23 21:58:58

int iArray[2][2] = {{1, 2}, {3, 4}};

将二维数组视为数组的数组。

int iArray[2][2] = {{1, 2}, {3, 4}};

Think of a 2D array as an array of arrays.

别挽留 2024-10-23 21:58:58

一种替代方法是将二维数组表示为一维数组。这可以使逐元素操作更加高效。您可能应该将其包装在一个也包含宽度和高度的类中。

另一种替代方法是将 2D 数组表示为 std::vector 。 >。这将允许您使用 STL 的算法进行数组运算,并且向量还将为您处理内存管理。

One alternative is to represent your 2D array as a 1D array. This can make element-wise operations more efficient. You should probably wrap it in a class that would also contain width and height.

Another alternative is to represent a 2D array as an std::vector<std::vector<int> >. This will let you use STL's algorithms for array arithmetic, and the vector will also take care of memory management for you.

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