C++ 如何在c ++中找到二维数组大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10274162/
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
how to find 2d array size in c++
提问by user1322915
How do I find the size of a 2D array in C++? Is there any predefined function like sizeof
to determine the size of the array?
如何在 C++ 中找到二维数组的大小?是否有任何预定义的函数sizeof
来确定数组的大小?
Also, can anyone tell me how to detect an error in the getvalue
method for arrays while trying to get a value which is not set?
另外,谁能告诉我如何getvalue
在尝试获取未设置的值时检测数组方法中的错误?
回答by SridharKritha
Suppose you were only allowed to use array then you could find the size of 2-d array by the following way.
假设你只被允许使用数组,那么你可以通过以下方式找到二维数组的大小。
int ary[][5] = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 0}
};
int rows = sizeof ary / sizeof ary[0]; // 2 rows
int cols = sizeof ary[0] / sizeof(int); // 5 cols
回答by Baz1nga
sizeof(yourObj)/sizeOf(yourObj[0])
should do the trick
应该做的伎俩
回答by ApprenticeHacker
Use an std::vector
.
使用std::vector
.
std::vector< std::vector<int> > my_array; /* 2D Array */
my_array.size(); /* size of y */
my_array[0].size(); /* size of x */
Or, if you can only use a good ol' array, you can use sizeof
.
或者,如果你只能使用一个好的 ol' 数组,你可以使用sizeof
.
sizeof( my_array ); /* y size */
sizeof( my_array[0] ); /* x size */
回答by bitbyter
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int arr[6][5] = {
{1,2,3,4,5},
{1,2,3,4,5},
{1,2,3,4,5},
{1,2,3,4,5},
{1,2,3,4,5},
{1,2,3,4,5}
};
int rows = sizeof(arr)/sizeof(arr[0]);
int cols = sizeof(arr[0])/sizeof(arr[0][0]);
cout<<rows<<" "<<cols<<endl;
return 0;
}
Output: 6 5
输出:6 5
回答by Ridowan Ahmed
#include<iostream>
using namespace std ;
int main()
{
int A[3][4] = { {1,2,3,4} , {4,5,7,8} , {9,10,11,12} } ;
for(int rows=0 ; rows<sizeof(A)/sizeof(*A) ; rows++)
{
for(int columns=0 ; columns< sizeof(*A) / sizeof(*A[0]) ; columns++)
{
cout<<A[rows][columns] <<"\t" ;
}
cout<<endl ;
}
}
回答by poorgoat
Along with the _countof() macro you can refer to the array size using pointer notation, where the array name by itself refers to the row, the indirection operator appended by the array name refers to the column.
与 _countof() 宏一起,您可以使用指针表示法引用数组大小,其中数组名称本身是指行,由数组名称附加的间接运算符是指列。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int beans[3][4]{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
cout << "Row size = " << _countof(beans) // Output row size
<< "\nColumn size = " << _countof(*beans); // Output column size
cout << endl;
// Used in a for loop with a pointer.
int(*pbeans)[4]{ beans };
for (int i{}; i < _countof(beans); ++i) {
cout << endl;
for (int j{}; j < _countof(*beans); ++j) {
cout << setw(4) << pbeans[i][j];
}
};
cout << endl;
}
回答by Osman Khalid
Here is one possible solution of first part
这是第一部分的一个可能的解决方案
#include<iostream>
using namespace std;
int main()
{
int marks[][4] = {
10, 20, 30, 50,
40, 50, 60, 60,
10, 20, 10, 70
};
int rows = sizeof(marks)/sizeof(marks[0]);
int cols = sizeof(marks)/(sizeof(int)*rows);
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
cout<<marks[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
回答by blank
int arr[5][4];
int arr[5][4];
For the row subscript(4 raise to 2, include cmath to use pow):
对于行下标(4 升至 2,包括 cmath 以使用pow):
sizeof(arr1)/pow(4,2)
Column subscript:
列下标:
sizeof(*arr1)/4
4 means 4 bytes, size of int.
4 表示 4 个字节,int 大小。
回答by pg30123
The other answers above have answered your first question. As for your second question, how to detect an error of getting a value that is not set, I am not sure which of the following situation you mean:
上面的其他答案已经回答了你的第一个问题。至于您的第二个问题,如何检测获取未设置值的错误,我不确定您指的是以下哪种情况:
Accessing an array element using an invalid index:
If you use std::vector, you can use vector::at function instead of [] operator to get the value, if the index is invalid, an out_of_range exception will be thrown.Accessing a valid index, but the element has not been set yet: As far as I know, there is no direct way of it. However, the following common practices can probably solve you problem: (1) Initializes all elements to a value that you are certain that is impossible to have. For example, if you are dealing with positive integers, set all elements to -1, so you know the value is not set yet when you find it being -1. (2). Simply use a bool array of the same size to indicate whether the element of the same index is set or not, this applies when all values are "possible".
使用无效索引访问数组元素:
如果使用 std::vector,则可以使用 vector::at 函数代替 [] 运算符来获取值,如果索引无效,则会抛出 out_of_range 异常。访问有效索引,但尚未设置元素:据我所知,没有直接的方法。但是,以下常见做法可能可以解决您的问题: (1) 将所有元素初始化为您确定不可能拥有的值。例如,如果您正在处理正整数,请将所有元素设置为 -1,这样当您发现该值是 -1 时,您就知道该值尚未设置。(2). 只需使用相同大小的 bool 数组来指示是否设置了相同索引的元素,这适用于所有值都“可能”的情况。