简体   繁体   English

如何计算 C++ 中数组的大小

[英]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.

相关问题 如何计算大小为 1000 x 1000 的二维数组中 2 个元素之间的步幅? C++ - How can I calculate the stride between 2 elements in a 2D array of size 1000 by 1000? C++ 我如何在C ++中将给定大小的数组改组并与另一个相同大小的数组进行比较 - how can i shuffle an array of a given size and compare it with another array of same size in C++ c ++:如何使用fftw3将数组转换为不同大小的其他数组 - c++ : how can I fourier transform an array into an other array of different size using fftw3 我可以在不设置大小的情况下使用 C++ 字符数组吗? - Can I use C++ char array without setting the size? C ++如何改进这个模板元程序,以回馈包含大小的数组? - C++ How can I improve this bit of template meta-program to give back the array including the size? 如何将元素添加到 C++ 中的非固定大小数组? - How can I add elements to a non-fixed size array in C++? 如何在不要求用户在 C++ 中输入大小的情况下创建动态数组? - How can I create dynamic array without asking user to enter a size in C++? C ++:如何计算方法的成本(算法分析) - C++ : How can I calculate a cost of a method (Algorithm Analysis) 如何使用C ++计算召回率和精度? - How can I calculate recall and precision using c++? 如何在 C++ 中计算对象的哈希/校验和/指纹? - How can I calculate a hash/checksum/fingerprint of an object in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM