如何在c++中找到2d数组大小

时间:2022-02-20 01:08:25

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++中找到2D数组的大小?是否有像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方法中的错误吗?

8 个解决方案

#1


20  

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

#2


14  

sizeof(yourObj)/sizeOf(yourObj[0])

should do the trick

应该足够了

#3


12  

Use an std::vector.

使用std::向量。

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 */

#4


4  

#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

#5


2  

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;
}

#6


1  

#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 ;
    }
}

#7


0  

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:

以上其他答案已经回答了你的第一个问题。关于你的第二个问题,如何检测获取未设置值的错误,我不确定你是指以下哪一种情况:

  1. 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.

    使用无效索引:::vector访问数组元素时,可以使用vector:::at函数而不是使用[]运算符来获取值,如果索引无效,将抛出out_of_range异常。

  2. 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".

    访问一个有效的索引,但是元素还没有设置:据我所知,没有直接的方法。但是,以下常见的实践可能会解决您的问题:(1)将所有元素初始化为您确信不可能拥有的值。例如,如果你在处理正整数,把所有的元素都设为-1,这样你就知道当你发现值为-1时,值还没有被设置。(2)简单地使用相同大小的bool数组来指示是否设置相同索引的元素,当所有值都是“可能的”时,就会使用这个数组。

#8


0  

int arr[5][4];

arr int[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字节,整数的大小。

#1


20  

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

#2


14  

sizeof(yourObj)/sizeOf(yourObj[0])

should do the trick

应该足够了

#3


12  

Use an std::vector.

使用std::向量。

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 */

#4


4  

#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

#5


2  

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;
}

#6


1  

#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 ;
    }
}

#7


0  

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:

以上其他答案已经回答了你的第一个问题。关于你的第二个问题,如何检测获取未设置值的错误,我不确定你是指以下哪一种情况:

  1. 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.

    使用无效索引:::vector访问数组元素时,可以使用vector:::at函数而不是使用[]运算符来获取值,如果索引无效,将抛出out_of_range异常。

  2. 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".

    访问一个有效的索引,但是元素还没有设置:据我所知,没有直接的方法。但是,以下常见的实践可能会解决您的问题:(1)将所有元素初始化为您确信不可能拥有的值。例如,如果你在处理正整数,把所有的元素都设为-1,这样你就知道当你发现值为-1时,值还没有被设置。(2)简单地使用相同大小的bool数组来指示是否设置相同索引的元素,当所有值都是“可能的”时,就会使用这个数组。

#8


0  

int arr[5][4];

arr int[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字节,整数的大小。