简体   繁体   English

如何在C ++中获取二维动态字符串数组的维数?

[英]How to get the dimensions of a 2d dynamic array of strings in C++?

I have a dynamically populated array of strings in C++: 我在C ++中有一个动态填充的字符串数组:

string** A;

it is populated like this: 它是这样填充的:

A = new string*[size1];

and then: 接着:

for (unsigned int i = 0; i < size1; i++)
{
    A[i] = new string[size2];
    for (unsigned int j = 0; j < size2; j++)
    {
        A[i][j] = whatever[j];
    }
}

elsewhere, I want to find out the dimensions (size1 and size2). 在其他地方,我想找出尺寸(尺寸1和尺寸2)。 I tries using this: 我尝试使用此:

sizeof(A[i]) / sizeof(A[i][0])

but it doesn't work. 但这不起作用。 Any ideas ? 有任何想法吗 ?

Thanks 谢谢

When you allocate memory via new T[N] , the value N is not stored anywhere . 通过new T[N]分配内存时,值N不会存储在任何地方。 If you need to know it later, you will need to keep track of it in your code. 如果以后需要知道它,则需要在代码中跟踪它。

There are pre-existing classes for allocating memory that also remember the length that was allocated. 有一些用于分配内存的预先存在的类,这些类也记住分配的长度。 In your code: 在您的代码中:

vector<vector<string>> A(size1, vector<string>(size2));
// (code to populate...)

then you can access A.size() to get size1 , and A[0].size() to get size2 . 那么你就可以访问A.size()来获得size1 ,和A[0].size()来获得size2

If the dimensions are known at compile-time you may use array instead of vector . 如果尺寸在编译时已知,则可以使用array而不是vector

It is very simple to find the size of a two dimensional (more exactly of one-dimensional dynamically allocated arrays) array. 查找二维(更确切地说是一维动态分配的数组)数组的大小非常简单。 Just declare it like 就像这样声明

std::vector<std::vector<std::string>> A;

and use 和使用

std::cout << A.size() << std::endl;

As for your approach then you have to store the sizes in some variables when the array is allocated. 至于您的方法,则必须在分配数组时将大小存储在某些变量中。

If you are learning C++, I would recommend that you learn Classes. 如果您正在学习C ++,我建议您学习类。 With a class you can encapsulate int variables along with your 2D array that you can use to store the dimensions of your array. 通过一个类,您可以将int变量与2D数组一起封装,以用于存储数组的维数。 For example: 例如:

class 2Darray{
 string **array;
 int rows;
 int cols;
}

You can then get the dimensions of your 2Darray object anytime by reading these member variables. 然后,您随时可以通过读取这些成员变量来获取2Darray对象的尺寸。

vectors will do this for you behind the scenes but its good for you to learn how to do this. vectors将在后台为您执行此操作,但对您学习如何执行此操作很有帮助。

You can't create an array just using pointer operator. 您不能仅使用指针运算符来创建数组。 Every array is basically a pointer with allocated memory. 每个数组基本上都是一个分配了内存的指针。 That's why compiler wants constant before creating array. 这就是为什么编译器在创建数组之前需要常量。

Basically; 基本上; sizeof(A[i]) won't give you the size of array. sizeof(A[i])不会给你数组的大小。 Because sizeof() function will return the a pointers size which is points to A[i] location. 因为sizeof()函数将返回一个指向A [i]位置的指针大小。 sizeof(A[i]) / sizeof(A[i][1]) will probably give you 1 because you are basically doing sizeof(int)/sizeof(int*) sizeof(A[i]) / sizeof(A[i][1])可能会给您1,因为您基本上是在做sizeof(int)/sizeof(int*)

So you need to store the boundary yourself or use vectors. 因此,您需要自己存储边界或使用向量。 I would prefer vectors. 我更喜欢矢量。

无法通过指针获得数组尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM