If you mean a C-style array, then you can do something like:
(如果您的意思是C样式的数组,则可以执行以下操作:)
int a[7];
std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;
This doesn't work on pointers (ie it won't work for either of the following):
(这不工作的指针(即,它会为以下任一不工作):)
int *p = new int[7];
std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
or:
(要么:)
void func(int *p)
{
std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
}
int a[7];
func(a);
In C++, if you want this kind of behavior, then you should be using a container class;
(在C ++中,如果您想要这种行为,则应该使用容器类。)
probably std::vector
. (可能是std::vector
。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…