二维数组值 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4981197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2D array values C++
提问by Kingkong Jnr
I wanted to declare a 2D array and assign values to it, without running a for loop.
我想声明一个二维数组并为其赋值,而无需运行 for 循环。
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 *
J:\CPP\Grid>bcc32.exe Grid.cpp
Borland C++ 5.5.1 for Win32 版权所有 (c) 1993, 2000 Borland
网格.cpp:
错误 E2188 Grid.cpp 11:函数 main() 中的表达式语法
错误 E2188 Grid.cpp 12:函数 main() 中的表达式语法
警告 W8004 Grid.cpp 14:'arr' 被分配了一个从未在函数 main() 中使用过的值
* 2 编译错误 *
Please help as to what is the right way to initialize the 2d array with my set of values.
请帮助了解使用我的值集初始化二维数组的正确方法是什么。
回答by Cheers and hth. - Alf
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?
这应该包含在您的 C++ 教科书中:您使用的是哪一本?
Anyway, better, consider using std::vector
or some ready-made matrix class e.g. from Boost.
无论如何,更好的是,考虑使用std::vector
或一些现成的矩阵类,例如来自 Boost。
回答by templatetypedef
The proper way to initialize a multidimensional array in C or C++ is
在 C 或 C++ 中初始化多维数组的正确方法是
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!
另外,在您的初始代码中要小心 - 您试图在数组中使用 1-indexed 偏移量来初始化它。这没有编译,但如果编译了它会导致问题,因为 C 数组是 0 索引的!
回答by pmg
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;
}
回答by Avery3R
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.
将二维数组视为数组数组。
回答by Dima
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.
另一种选择是将二维数组表示为std::vector<std::vector<int> >
. 这将允许您使用 STL 的算法进行数组运算,并且向量还将为您处理内存管理。