31

如何在 C++ 中找到二维数组的大小?是否有任何预定义的函数sizeof来确定数组的大小?

另外,谁能告诉我如何getvalue在尝试获取未设置的值时检测数组方法中的错误?

4

11 回答 11

56

假设您只被允许使用数组,那么您可以通过以下方式找到二维数组的大小。

  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
于 2014-07-27T19:19:22.940 回答
19
#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;
}

输出:6 5

于 2015-09-27T21:34:51.147 回答
17

使用std::vector.

std::vector< std::vector<int> > my_array; /* 2D Array */

my_array.size(); /* size of y */
my_array[0].size(); /* size of x */

或者,如果你只能使用一个好的 ol' 数组,你可以使用sizeof.

sizeof( my_array ); /* y size */
sizeof( my_array[0] ); /* x size */
于 2012-04-23T02:36:31.500 回答
16
sizeof(yourObj)/sizeOf(yourObj[0])

应该做的伎俩

于 2012-04-23T02:35:46.207 回答
2

与 _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;
}
于 2017-07-24T05:52:39.340 回答
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 ;
    }
}
于 2015-02-19T20:06:38.197 回答
1

这是第一部分的一个可能的解决方案

#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;
}
于 2020-04-21T13:23:36.487 回答
0

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

  1. 使用无效索引访问数组元素:
    如果使用std::vector,可以使用vector::at函数代替[]运算符来获取值,如果索引无效,则会抛出out_of_range异常。

  2. 访问有效索引,但尚未设置元素:据我所知,没有直接的方法。但是,以下常见做法可能会解决您的问题: (1) 将所有元素初始化为您确定不可能拥有的值。例如,如果您正在处理正整数,请将所有元素设置为 -1,这样当您发现它为 -1 时,您就知道该值尚未设置。(2)。只需使用相同大小的 bool 数组来指示是否设置了相同索引的元素,这适用于所有值“可能”的情况。

于 2012-04-23T03:53:31.917 回答
0

int arr[5][4];

对于行下标(4 提高到 2,包括 cmath 以使用pow):

sizeof(arr1)/pow(4,2)   

列下标:

sizeof(*arr1)/4

4 表示 4 个字节,大小为 int。

于 2016-08-14T14:23:16.477 回答
0

这也可以尝试获取二维数组的行列大小

matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 
int rows =  matrix.size();
int cols = matrix[0].size();
cout<<rows<< " "<<cols;    

输出:3 4

于 2021-05-08T07:05:38.133 回答
0
int rows = sizeof(arr)/sizeof(arr[0]);
int cols = sizeof(arr[0])/sizeof(arr[0][0]);
于 2021-07-03T03:34:17.517 回答