c++获取数组长度查找算法
介绍 (Introduction)
In this article, we are going to learn about the various ways following which we can find array length in C++.
在本文中,我们将学习在C ++中可以找到数组长度的各种方法。
Basically, when we say the length of an array actually we refer to the total number of elements present in the corresponding array. For example, for an array as given below:
基本上,当我们说一个数组的长度时,实际上是指相应数组中存在的元素总数。 例如,对于下面给出的数组:
int array1[] = { 0, 1, 2, 3, 4 }
The size of the array or length of the array here is equal to the total number of elements in it. Which, in this case, is ‘5‘.
此处数组的大小或数组的长度等于数组中元素的总数。 在这种情况下为“ 5 ”。
在C ++中查找数组长度的方法 (Ways to find Length of an Array in C++)
Now let us take a look at the different ways following which we can find the length of an array in C++, they are as follow:
现在让我们看一下在C ++中可以找到数组长度的不同方法,它们如下:
- Counting element-by-element, 逐元素计数
begin()andend(),begin()和end(),sizeof()function,sizeof()函数,size()function in STL, STL中的size()函数,- Pointers. 指针。
Now, let us discuss each method one-by-one with examples and in detail.
现在,让我们与示例逐一讨论详细的方法。
1.逐元素计数 (1. Counting element-by-element)
Traversing throughout the given array and simultaneously counting the total number of elements we traversed can give us the length of the array right?
在给定数组中遍历并同时计算遍历的元素总数可以使我们知道数组的长度对吗?
But for traversing we cannot use a for loop directly if we do not know the array length beforehand. This issue can be solved by using a simple for-each loop. Look at the code below carefully.
但是对于遍历,如果我们事先不知道数组长度,则不能直接使用for循环。 通过使用简单的for-each循环可以解决此问题。 仔细看下面的代码。
#include<iostream>
#include<array>
using namespace std;
int main()
{
int c;
int arr[]={1,2,3,4,5,6,7,8,9,0};
cout<<"The array is: ";
for(auto i: arr)
{
cout<<i<<" ";
c++;
}
cout<<"\nThe length of the given Array is: "<<c;
return 0;
}
Output:
输出:
The array is: 1 2 3 4 5 6 7 8 9 0
The length of the given Array is: 10
Here, as we said, we traverse through the entire array arr using a for-each loop with iterator i. Simultaneously the counter, c is incremented. At last, when the traversing is over, c holds the length of the given array.
正如我们所说,这里我们使用带有迭代器i的for-each循环遍历整个数组arr。 同时,计数器c递增。 最后,当遍历结束时, c保留给定数组的长度。
2.使用begin()和end() (2. Using begin() and end())
We can also calculate the length of an array using the standard library’s begin() and end() functions. The two functions return iterators pointing to the start and the end of the corresponding array respectively. Take a close look at the given code,
我们还可以使用标准库的begin()和end()函数来计算数组的长度。 这两个函数返回分别指向相应数组开始和结束的迭代器。 仔细看看给定的代码,
#include<iostream>
#include<array>
using namespace std;
int main()
{
//Given Array
int arr[] = { 11, 22, 33, 44 };
cout<<"The Length of the Array is : "<<end(arr)-begin(arr); //length
return 0;
}
Output:
输出:
The Length of the Array is : 4
Hence, here as we can see, the difference between the return values of the two functions end() and begin() give us the size or length of the given array, arr. That is, in our case, 4.
因此,在这里我们可以看到,两个函数end()和begin()的返回值之间的差为我们提供了给定数组arr的大小或长度。 也就是说,在我们的例子中是4 。
3.在C ++中使用sizeof()函数查找数组长度 (3. Using sizeof() function to Find Array Length in C++)
The sizeof() operator in C++ returns the size of the passed variable or data in bytes. Similarly, it returns the total number of bytes required to store an array too. Hence, if we simply divide the size of the array by the size acquired by each element of the same, we can get the total number of elements present in the array.
C ++中的sizeof()运算符返回传递的变量或数据的大小(以字节为单位)。 同样,它也返回存储数组所需的字节总数。 因此,如果我们简单地将数组的大小除以相同元素每个元素获取的大小,就可以获得数组中存在的元素总数。
Let us how that works
让我们看看它是如何工作的
#include<iostream>
#include<array>
using namespace std;
int main()
{ //Given array
int arr[] = {10 ,20 ,30};
int al = sizeof(arr)/sizeof(arr[0]); //length calculation
cout << "The length of the array is: " <<al;
return 0;
}
Output:
输出:
The length of the array is: 3
As we can see, we get our desired output.
如我们所见,我们获得了所需的输出。
4.在STL中使用size()函数 (4. Using the size() function in STL)
We have the size() function defined in the standard library which returns the number of elements in the given container(array in our case).
我们在标准库中定义了size()函数,该函数返回给定容器(在本例中为数组)中元素的数量。
#include<iostream>
#include<array>
using namespace std;
int main()
{ //Given array
array<int,5> arr{ 1, 2, 3, 4, 5 };
//Using the size() function from STL
cout<<"\nThe length of the given Array is: "<<arr.size();
return 0;
}
Output:
输出:
The length of the given Array is: 5
As desired, we get the output as shown above.
根据需要,我们得到如上所示的输出。
5.使用指针在C ++中查找数组长度 (5. Using Pointers to Find Array Length in C++)
We can also find the length of a given array using pointers. Let us see how
我们还可以使用指针找到给定数组的长度。 让我们看看
#include<iostream>
#include<array>
using namespace std;
int main()
{ //Given array
int arr[6] = {5,4,3,2,1,0};
int len = *(&arr + 1) - arr;
//*(&arr + 1) is the address of the next memory location
// just after the last element of the array
cout << "The length of the array is: " << len;
return 0;
}
Output:
输出:
The length of the array is: 6
The expression *(arr+1) gives us the address of the memory space just after the array’s last element. Hence, the difference between it and the arrays starting location or the base address(arr) gives us the total number of elements present in the given array.
表达式*(arr+1)为我们提供了数组最后一个元素之后的内存空间地址。 因此,它与数组起始位置或基地址( arr )之间的差异为我们提供了给定数组中存在的元素总数。
结论 (Conclusion)
So, in this tutorial, we discussed the various methods to find array length in C++. Even though each one of the above examples is easy to go with, we prefer the one using the for-each loop. Not only because of code readability but also for its cross-platform reliability.
因此,在本教程中,我们讨论了在C ++中查找数组长度的各种方法。 即使上面的每个示例都很容易使用,我们还是更喜欢使用for-each循环的示例。 不仅由于代码可读性,而且还因为其跨平台的可靠性。
For any further questions feel free to use the comments below.
如有任何其他疑问,请随时使用以下评论。
参考资料 (References)
- How do I find the length of an array? – Stack Overflow question, 如何找到数组的长度? –堆栈溢出问题,
- for-each loop in C++ – JournalDev post. C ++中的for-each循环 – JournalDev发布。
翻译自: https://www.journaldev.com/37594/find-array-length-in-c-plus-plus
c++获取数组长度查找算法

491

被折叠的 条评论
为什么被折叠?



