How to get the size of an Array? [duplicate]

ε祈祈猫儿з 提交于 2019-12-04 19:32:24

问题


In C# I use the Length property embedded to the array I'd like to get the size of. How to do that in C++?


回答1:


Arrays in C/C++ do not store their lengths in memory, so it is impossible to find their size purely given a pointer to an array. Any code using arrays in those languages relies on a constant known size, or a separate variable being passed around that specifies their size.

A common solution to this, if it does present a problem, is to use the std::vector class from the standard library, which is much closer to a managed (C#) array, i.e. stores its length and additionally has a few useful member functions (for searching and manipulation).

Using std::vector, you can simply call vector.size() to get its size/length.




回答2:


It really depends what you mean by "array". Arrays in C++ will have a size (meaning the "raw" byte-size now) that equals to N times the size of one item. By that one can easily get the number of items using the sizeof operator. But this requires that you still have access to the type of that array. Once you pass it to functions, it will be converted to pointers, and then you are lost. No size can be determined anymore. You will have to construct some other way that relies on the value of the elements to calculate the size.

Here are some examples:

int a[5];
size_t size = (sizeof a / sizeof a[0]); // size == 5

int *pa = a; 

If we now lose the name "a" (and therefor its type), for example by passing "pa" to a function where that function only then has the value of that pointer, then we are out of luck. We then cannot receive the size anymore. We would need to pass the size along with the pointer to that function.

The same restrictions apply when we get an array by using new. It returns a pointer pointing to that array's elements, and thus the size will be lost.

int *p = new int[5];
  // can't get the size of the array p points to. 
delete[] p;

It can't return a pointer that has the type of the array incorporated, because the size of the array created with new can be calculated at runtime. But types in C++ must be set at compile-time. Thus, new erases that array part, and returns a pointer to the elements instead. Note that you don't need to mess with new in C++. You can use the std::vector template, as recommended by another answer.




回答3:


To count the number of elements in a static array, you can create a template function:

template < typename T, size_t N >
size_t countof( T const (&array)[ N ] )
{
    return N;
} 

For standard containers such as std::vector, the size() function is used. This pattern is also used with boost arrays, which are fixed size arrays and claim no worse performance to static arrays. The code you have in a comment above should be:

for ( std::vector::size_type i(0); i < entries.size(); ++i )

( assuming the size changes in the loop, otherwise hoist it, ) rather than treating size as a member variable.




回答4:


In C/C++, arrays are simply pointers to the first element in the array, so there is no way to keep track of the size or # of elements. You will have to pass an integer indicating the size of the array if you need to use it.

Strings may have their length determined, assuming they are null terminated, by using the strlen() function, but that simply counts until the \0 character.




回答5:


Als Nolrodin pointed out above, it is pretty much impossible to get the size of an plain array in C++ if you only have a pointer to its first element. However if you have a fixed-size array there is a well-known C trick to work out the number of elements in the array at compile time, namely by doing:

GrmblFx loadsOfElements[1027];
GrmblFx_length = sizeof(loadsOfElements)/sizeof(GrmblFx);



来源:https://stackoverflow.com/questions/874169/how-to-get-the-size-of-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!