如何快速找到a)静态阵列b)动态数组中存储的元素数量? [英] How can you quickly find the number of elements stored in a a) staticarray b) dynamic array ?

查看:92
本文介绍了如何快速找到a)静态阵列b)动态数组中存储的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有这个面试问题请回复。


如何快速找到存储在aa中的元素数量静态

数组b)动态数组?


Rgrds

MA

Hi all, got this interview question please respond.

How can you quickly find the number of elements stored in a a) static
array b) dynamic array ?

Rgrds
MA

推荐答案

C C ++ C ++写道:
C C++ C++ wrote:

大家好,有这个面试问题请回复。


如何快速找到存储的元素数量aa)静态

数组b)动态数组?
Hi all, got this interview question please respond.

How can you quickly find the number of elements stored in a a) static
array b) dynamic array ?



你没有提到,静态数组和动态数组是如何给你的b
。因此,我将做一些假设。


对于固定大小的数组,您可以使用模板:


template< typename T,unsigned long N>

无符号长度长度(T const(&)[N]){

return(N);

}


#include< iostream>


int main(void){

char arr [ 20];

std :: cout<<长度(arr)<< ''\\ n';;

}


另一方面,不应该有这个:你真的想

避免像20这样的幻数。因此,你只需编写代码如下:

static const unsigned int my_array_size = 20;


int main(void){

char arr [my_array_size];

std :: cout<< my_array_size<< ''\ n'';

}

对于用new []动态分配的T数组并存储在变量中

of输入T *,你只是运气不好:虽然编译器必须保持数组大小的跟踪(至少如果T有一个非平凡的析构函数和

)有一个相应的删除[]),标准C ++中没有办法在这条信息上获得

。因此,您需要手动存储长度。

如果T是可复制构造且可分配的,您当然应该使用

std :: vector。这将跟踪你的大小。


注意:有可能使用用户定义的一些技巧

new-handler或者一些这样的事情,但我不知道如何做到这一点和

无论如何我怀疑这是个好主意。

Best


Kai-Uwe Bux

You do not mention, how the static array and the dynamic array are given to
you. Therefore, I will make some assumptions.

For an array of fixed size, you can use templates:

template < typename T, unsigned long N >
unsigned long length ( T const (&) [N] ) {
return ( N );
}

#include <iostream>

int main ( void ) {
char arr [20];
std::cout << length( arr ) << ''\n'';
}

On the other hand, there shouldn''t be a need for this: you really want to
avoid magic numbers like 20. Thus, you would simply write the code like so:

static const unsigned int my_array_size = 20;

int main ( void ) {
char arr [ my_array_size ];
std::cout << my_array_size << ''\n'';
}
For an array of T dynamically allocated with new[] and stored in a variable
of type T*, you are simply out of luck: although the compiler has to keep
track of the array size (at least if T has a non-trivial destructor and
there is a corresponding delete[]), there is no way in standard C++ to get
at that piece of information. Thus, you need to store the length manually.
If T is copy-constructible and assignable, you should of course use
std::vector instead. That will keep track of the size for you.

Note: It is possible that one could use some trickery with a user defined
new-handler or some such thing, but I don''t know off hand how to do it and
I doubt that it would be a good idea anyway.
Best

Kai-Uwe Bux


2008-01-12 15:05,C C ++ C ++写道:
On 2008-01-12 15:05, C C++ C++ wrote:

大家好,有这个面试问题请回复。


如何快速找到存储在aa中的元素数量静态

数组b)动态数组?
Hi all, got this interview question please respond.

How can you quickly find the number of elements stored in a a) static
array b) dynamic array ?



两者的简单答案是检查包含大小的变量。

更多参与答案:静态分配数组的大小必须在编译时已知
因此大小始终是已知的。您可以使用这样的

结构来获得它的大小:


int a [5];

std :: cout << sizeof(a)/ sizeof(int);


对于一个动态分配的数组,你所拥有的只是指向第一个

元素的指针,如果你没有在数组末尾使用一些特殊元素

你无法确定大小,这意味着你必须传递

大小以及指向第一个元素的指针。


另请注意,除非你使用一些模板技巧,否则静态分配的数组会在传递时衰减为指向其第一个元素的指针

到一个函数,这意味着你和

动态分配的数组的情况相同:你需要传递大小或者有一个特殊的

终止元素。


当然当被问到这样一个问题时你应该问提问者为什么

他们没有使用std :: vector或类似的而不是使用原始数组。


-

Erik Wikstr ?? m

The simple answer to both is to check the variable containing the size.
More involved answer: The size of a statically allocated array must be
known at compile-time so the size is always known. You can use a
construct like this to get its size:

int a[5];
std::cout << sizeof(a) / sizeof(int);

For a dynamically allocated array all you have is a pointer to the first
element, if you do not use some special element at the end of the array
you can not determine the size, this means that you have to pass the
size along with the pointer to the first element.

Note also that unless you use some template tricks a statically
allocated arrays decays into a pointer to its first element when passed
to a function, which means that you are in the same situation as with a
dynamically allocated array: you need to pass the size or have a special
terminating element.

Of course when asked such a question you should ask the questioner why
they did not use std::vector or similar instead of using a raw array.

--
Erik Wikstr??m


1月12日,7:05 pm,C C ++ C ++ < m.azm ... @ gmail.comwrote:
On Jan 12, 7:05 pm, "C C++ C++" <m.azm...@gmail.comwrote:

大家好,有这个面试问题请回复。


如何快速找到存储在aa中的元素数量静态

数组b)动态数组?


Rgrds

MA
Hi all, got this interview question please respond.

How can you quickly find the number of elements stored in a a) static
array b) dynamic array ?

Rgrds
MA



如果数组命名为[]

那么

for(i = 0; i! =''\'''; i ++)

{

cout<< a [i];

}

cout<< i; //这将显示数组的长度

say if the array named a[]
then
for(i=0;i!=''\0'';i++)
{
cout<<a[i];
}
cout<<i; //this will show u the length of array


这篇关于如何快速找到a)静态阵列b)动态数组中存储的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆