[英]How can I calculate size of Array in C++
I had the online coding interview today and I really struggled while trying to calculate the size of the array.我今天进行了在线编码面试,在尝试计算数组的大小时我真的很吃力。 Could you please help me with how can I measure the sizeof array here?
您能帮我解决一下如何在这里测量 sizeof 数组吗? I tried my best but no luck please help here.
我尽力了,但没有运气,请在这里帮忙。
#include<iostream>
#include<map>
#include<vector>
using namespace std;
void arraysize(int* a) {
cout << "size1: "<<sizeof(a) << endl;
cout << "size2: " << sizeof(a[0]) << endl;;
}
int main()
{
int array1[] = { 1,2,3,4,5,6,7,8 };
arraysize(array1);
return 0;
}
Result: size1: 4 size2: 4结果:尺寸1:4 尺寸2:4
In most cases, when you pass an array to a function, the array will be converted to a pointer.在大多数情况下,当您将数组传递给 function 时,该数组将被转换为指针。 This is called an array-to-pointer decay .
这称为数组到指针衰减。 Once this decay happens, you lose the size information of the array.
一旦发生这种衰减,您就会丢失数组的大小信息。 That is, you can no longer tell the size of the original array from the pointer.
也就是说,您不能再从指针中分辨出原始数组的大小。
However, one case in which this conversion / decay will not happen is when we pass a reference to the array.但是,这种转换/衰减不会发生的一种情况是当我们传递对数组的引用时。 We can take advantage of this property to get the size of an array.
我们可以利用这个属性来获取数组的大小。
#include<iostream>
template<typename T, size_t N>
size_t asize(T (&array)[N])
{
return N;
}
int main()
{
int array1[] = { 1,2,3,4,5,6,7,8 };
std::cout << asize(array1) << std::endl; // 8
return 0;
}
In the above case, to the template function asize
, we pass a reference to an array of type T[N]
, whose size is N
.在上面的例子中,我们向模板 function
asize
传递一个对类型为T[N]
的数组的引用,该数组的大小为N
。 In this case, it is array type int[8]
.在这种情况下,它是数组类型
int[8]
。 So the function returns N
, which is size 8.所以 function 返回
N
,大小为 8。
C
style array
's decay to pointer
's when passed to a function like this.当像这样传递给 function 时,
C
样式array
衰减到pointer
。 The first cout
statement is printing the size of a pointer
on your machine.第一个
cout
语句是在您的机器上打印pointer
的大小。 The second cout
statement is printing the size of an integer
.第二个
cout
语句正在打印integer
的大小。
Use one of the following solutions in order to pass the size of the array
to the function.使用以下解决方案之一将
array
的大小传递给 function。
template<std::size_t N>
void ArraySize( int ( &array )[ N ] )
{
std::cout << "Array size: " << N << '\n';
}
void ArraySize( int* array, std::size_t size )
{
std::cout << "Array size: " << size << '\n';
}
template<std::size_t N>
void ArraySize( std::array<int, N>& array )
{
std::cout << "Array size: "<< array.size( ) << '\n';
}
sizeof(a) returns the number of bytes in array, sizeof(int) returns the number of bytes in an int, ergo sizeof(a)/sizeof(int) returns the array length sizeof(a) 返回数组中的字节数,sizeof(int) 返回 int 中的字节数,因此 sizeof(a)/sizeof(int) 返回数组长度
Easiest way to get the size of an array:获取数组大小的最简单方法:
#include <iostream>
using namespace std;
int main(void) {
int ch[5], size;
size = sizeof(ch) / sizeof(ch[0]);
cout << size;
return 0;
}
Output: 5
Output:5
simply divide sizeof(array1)
by sizeof(int)
.只需将
sizeof(array1)
除以sizeof(int)
即可。 it will give you total element in array.它会给你数组中的总元素。 because
sizeof(array1)
will give total bytes in the array.因为
sizeof(array1)
将给出数组中的总字节数。 for example sizeof(array1)
= int * 8 because your array is int so int is 4 byte answer is 4*8 = 32.Now you have to divide it again by 4 cause its in byte.例如
sizeof(array1)
= int * 8 因为你的数组是 int 所以 int 是 4 字节答案是 4*8 = 32。现在你必须再次将它除以 4 导致它的字节。
cout << "Size of the Array is : " << sizeof(array1)/sizeof(int) << endl;
put above code in your main function to get result把上面的代码放在你的主 function 得到结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.