[英]How to get number of elements in the array of pointers?
How to get number of elements in array of pointers? 如何获取指针数组中的元素数量?
Below is my code: 以下是我的代码:
struct mystruct
{
int a;
char ch[10];
int c;
};
mystruct *m_arr[2];
I am traversing this array in some other files. 我在其他一些文件中遍历此数组。 Instead of hard-coding as 2 in every file I want to get the number of elements in the array programmatically. 我没有在每个文件中硬编码为2,而是想以编程方式获取数组中的元素数量。
Don't use raw arrays. 不要使用原始数组。 Use a standard container like std::vector
or std::array
. 使用标准容器,如std::vector
或std::array
。 Both of these have a .size()
member, and allow the range-based for syntax: 这两个都有一个.size()
成员,并允许基于范围的语法:
for (mystruct* p : m_arr)
If you need C compatability, they both offer a data()
member function which returns a pointer to the first element in the underlying array. 如果你需要C兼容性,它们都提供了一个data()
成员函数,它返回一个指向底层数组中第一个元素的指针。 (In your case, that will be a mystruct **
) (在你的情况下,这将是一个mystruct **
)
Edit: A raw array also supports the range-based for syntax - but only if the visible declaration includes the number of elements (so my_struct* m_arr[2];
is fine, but my_struct* m_arr[]
would not work). 编辑:原始数组也支持基于范围的语法 - 但仅当可见声明包含元素数量时(因此my_struct* m_arr[2];
很好,但my_struct* m_arr[]
不起作用)。 It is impossible to declare a std::array
without defining the size too. 如果没有定义大小,也不可能声明std::array
。 Other containers (like std::vector
) don't include the size in the declaration. 其他容器(如std::vector
)不包括声明中的大小。
The usual way of doing this is: 通常的做法是:
size_t sizeOfArray = sizeof(m_arr)/sizeof(m_arr[0]);
About size_t
from wiki : 关于wiki的 size_t
:
size_t is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. size_t是一个无符号整数类型,用于表示特定实现中任何对象(包括数组)的大小。 The sizeof operator yields a value of the type size_t . sizeof运算符产生size_t类型的值 。 The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the header (cstdint header in C++). size_t的最大大小是通过SIZE_MAX提供的,SIZE_MAX是一个宏常量,它在头文件中定义(C ++中的cstdint头)。 size_t is guaranteed to be at least 16 bits wide. size_t保证至少为16位宽。
If this is a header file included by all other source code files using m_arr
you can use sizeof(m_arr)/sizeof(m_arr[0])
to obtain the number of elements in the array. 如果这是使用m_arr
的所有其他源代码文件包含的头文件,则可以使用sizeof(m_arr)/sizeof(m_arr[0])
来获取数组中的元素数。 But this is really dangerous. 但这真的很危险。 If at some point the pointer m_arr
enters a function as a parameter the scope of the function will not return 2 at sizeof(m_arr)
but the number of bytes which the pointer takes in the memory which probably is 8. 如果在某个时刻指针m_arr
作为参数输入函数,则函数的作用域不会在sizeof(m_arr)
处返回2,而是指针在内存中占用的字节数,可能是8。
So if you want to stick to plain C then you have to pass the number of elements in a separate variable. 因此,如果您想坚持使用普通C,那么您必须在单独的变量中传递元素的数量。 But if you can use C++ there is a variety of better, safer and even faster solutions. 但是,如果你可以使用C ++,那么有各种更好,更安全甚至更快的解决方案。
You can always do such memory arithmetics: 你总是可以做这样的记忆算术:
size_t arraySize = sizeof(m_arr) / sizeof(m_arr[0]);
But if you don't have a reason like in When would you use an array rather than a vector/string? 但是如果你没有这样的理由, 你何时会使用数组而不是矢量/字符串? : :
Then you should use a std::array<mystruct*, 2> m_arr;
然后你应该使用std::array<mystruct*, 2> m_arr;
, the size of which you can access through m_arr.size()
, and the address of the underlying native array you get with m_arr.data()
. ,您可以通过m_arr.size()
访问的大小,以及使用m_arr.data()
获取的基础本机数组的地址。
在C ++ 17中,您可以#include <iterator>
并使用std::size(m_arr)
。
Yes, use std::vector, but if you must... 是的,使用std :: vector,但如果你必须......
this works for c++11 or greater: 这适用于c ++ 11或更高版本:
template <typename T, std::size_t N>
constexpr std::size_t sizeofArray(T(&)[N]) {
return N;
}
Saw this recently in a video about 7 Features of C++ You Can Adopt Today 最近在一个关于你今天可以采用的C ++的7个特性的视频中看到了这一点
Assuming that you have your array as the following: 假设您的数组如下:
char *array[3];
Add an extra item to the array, which is considered to serve as an array terminator. 向数组添加一个额外的项目,该数组被视为数组终止符。
char *array[3]={"First element","Second element",""};
The rest of it depends on your own coding style, you can check for the terminator and count the number of elements except the terminating string. 其余部分取决于您自己的编码风格,您可以检查终结符并计算除终止字符串之外的元素数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.