[英]C++ Linear search algorithm, determining the number of elements in array
This code is from the Geeks for Geeks Algorithms section and i do not understand this part此代码来自 Geeks for Geeks Algorithms 部分,我不明白这部分
int n = sizeof(arr) / sizeof(arr[0]);
In the main function, specifially why the division with sizeof(arr[0]) which would lead to half the number of actual elements in the array.在主要的 function 中,特别是为什么用 sizeof(arr[0]) 进行除法会导致数组中实际元素数量的一半。 Hope someone can explain me this.
希望有人可以向我解释这一点。
// C++ code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
#include <iostream>
using namespace std;
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
(result == -1)? cout<<"Element is not present in array"
: cout<<"Element is present at index " <<result;
return 0;
}
Let's consider the declared array让我们考虑声明的数组
int arr[] = { 2, 3, 4, 10, 40 };
As the size of the array is not specified then the number of elements in the array is equal to the number of initializers.由于未指定数组的大小,因此数组中的元素数等于初始值设定项的数量。
It is not difficult to count the number of elements.计算元素的数量并不难。 It is equal to
5
.它等于
5
。 So the array consists from 5 elements of the type int
.所以该数组由 5 个
int
类型的元素组成。
The memory allocated to the array is equal to 5 * sizeof( int )
.分配给数组的 memory 等于
5 * sizeof( int )
。 it is the same as 5 * sizeof( arr[0] )
because the element arr[0]
has the type int
.它与
5 * sizeof( arr[0] )
相同,因为元素arr[0]
的类型为int
。
so you have that the size of the whole arrau is所以你有整个arrau的大小是
sizeof( arr ) = 5 * sizeof( arr[0] )
Having this formula it is easy to determine the number of elements in the array having its size and the size of stored elements.有了这个公式,就很容易确定数组中具有其大小的元素数量和存储元素的大小。 That is
那是
5 = sizeof( arr ) / sizeof( arr[0] )
So having an arbitrary array arr
you can determine the number of elements the following way因此,拥有任意数组
arr
您可以通过以下方式确定元素的数量
N = sizeof( arr ) / sizeof( arr[0] )
The function search expects that the second argument will specify the number of elements in the passed array function 搜索期望第二个参数将指定传递数组中的元素数
int search(int arr[], int n, int x);
And it is calculated as shown above that is并且计算如上图即为
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
If you compiler supports the C++ 17 Standard then instead of using this formula you could use the standard function std::size
like如果您的编译器支持 C++ 17 标准,那么您可以使用标准 function
std::size
而不是使用此公式
int result = search(arr, std::size( arr ), x);
Pay attention to that the function declaration and definition is bad.注意 function 声明和定义是错误的。
For example the expression sizeof(arr) / sizeof(arr[0])
has the type size_t
but the corresponding function parameter has the type int
.例如,表达式
sizeof(arr) / sizeof(arr[0])
的类型为size_t
但对应的 function 参数的类型为int
。 The type int
can be not enough large to store values of the type size_t
. int
类型可能不够大,无法存储size_t
类型的值。 Secondly as the array is not being changed in the function then the first parameter shall have the qualifier const
.其次,由于 function 中的数组未更改,因此第一个参数应具有限定符
const
。
The function can be declared and defined the following way function可以通过以下方式声明和定义
size_t search( const int arr[], size_t n, int x)
{
size_t i = 0;
while ( i != n && arr[i] != x ) ++i;
return i;
}
If the target value is not found in the array then the function returns the size of the array that is n.如果在数组中找不到目标值,则 function 返回数组的大小,即 n。
Pay attention that there is standard algorithm std::find
that can be used instead of the function.请注意,可以使用标准算法
std::find
代替 function。
sizeof()
returns the memory used by the element. sizeof()
返回元素使用的 memory。
Lets assume c++
uses x bytes for int
.让我们假设
c++
使用 x 字节作为int
。
So int
array with 5 element will be of size 5x.因此,具有 5 个元素的
int
数组的大小将是 5x。
sizeof(arr)
returns the memory used by integer array with 5 elements which is 5x. sizeof(arr)
返回 integer 数组使用的 memory,其中 5 个元素为 5x。
sizeof(arr[0])
returns the memory used by integer which is x. sizeof(arr[0])
返回 integer 使用的 memory,即 x。
So sizeof(arr) / sizeof(arr[0])
will return 5 as the size所以
sizeof(arr) / sizeof(arr[0])
将返回 5 作为大小
The statement int n = sizeof(arr) / sizeof(arr[0]);
语句
int n = sizeof(arr) / sizeof(arr[0]);
is used to find the number of elements in arr
.用于查找
arr
中的元素数。
sizeof(arr)
- This will return the amount of memory used by arr
in bytes. sizeof(arr)
- 这将返回arr
使用的 memory 的字节数。 In our case, arr
is an array and so the expression will return the size occupied by arr
in bytes.在我们的例子中,
arr
是一个数组,因此表达式将返回arr
占用的大小(以字节为单位)。
sizeof(arr[0])
- This will also return the amount of memory stored by arr[0]
( Note : its not arr
). sizeof(arr[0])
- 这也将返回arr[0]
存储的 memory 的数量(注意:它不是arr
)。 In our case, arr[0]
is an element of array and so the expression will return the size occupied by arr[0]
in bytes.在我们的例子中,
arr[0]
是数组的一个元素,因此表达式将返回arr[0]
占用的大小(以字节为单位)。
Now, the sizeof(arr) / sizeof(arr[0]
will return the number of elements in the array. How? - The sizeof(arr)
gave us total size of array in bytes and sizeof(arr[0])
gave us the size of just 1 element. So dividing them, we get the number of elements in our array.现在,
sizeof(arr) / sizeof(arr[0]
将返回数组中元素的数量。如何? - sizeof(arr)
给了我们数组的总大小(以字节为单位), sizeof(arr[0])
给了我们只有 1 个元素的大小。因此,将它们分开,我们得到数组中元素的数量。
NOTE: the size of each element will be same, so it is not that you only have to choose arr[0]
.注意:每个元素的大小都是一样的,所以并不是你只需要选择
arr[0]
。 You could have chosen arr[3]
or any other index given it was within array bounds您可以选择
arr[3]
或任何其他索引,因为它在数组范围内
Hope this clears your doubt !希望这可以消除您的疑问!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.