有没有办法找出一个数组有多少个值?检测是否已经到达数组的末尾也可以工作。
当前回答
这是一个非常古老和传奇的问题,已经有很多惊人的答案。但是随着时间的推移,新的功能被添加到语言中,所以我们需要根据可用的新功能不断更新。
我只是注意到还没有人提到c++ 20。所以想写答案。
C + + 20
在c++ 20中,标准库中增加了一种新的更好的方法来查找数组的长度,即std:ssize()。这个函数返回一个带符号的值。
#include <iostream>
int main() {
int arr[] = {1, 2, 3};
std::cout << std::ssize(arr);
return 0;
}
C++17
在c++ 17中,(当时)有一个更好的方法来实现same,即在iterator中定义std::size()。
#include <iostream>
#include <iterator> // required for std::size
int main(){
int arr[] = {1, 2, 3};
std::cout << "Size is " << std::size(arr);
return 0;
}
附注:这个方法也适用于矢量。
Old
这种传统的方法已经在许多其他的答案中提到过。
#include <iostream>
int main() {
int array[] = { 1, 2, 3 };
std::cout << sizeof(array) / sizeof(array[0]);
return 0;
}
仅供参考,如果你想知道为什么这种方法不工作时,数组传递给另一个函数。原因是,
在c++中,数组不是按值传递的,而是传递指向数组的指针。在某些情况下,传递整个数组可能是昂贵的操作。你可以通过将数组传递给某个函数来测试这一点,并对那里的数组进行一些更改,然后再次在main中打印数组。你会得到更新的结果。
正如你已经知道的,sizeof()函数给出了字节数,所以在其他函数中,它将返回分配给指针的字节数,而不是整个数组。所以这种方法行不通。
但我相信你能根据你的要求找到一个好办法。
快乐的编码。
其他回答
你可以简单地使用这段代码:
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{
array<int,3> values;
cout << "No. elements in valuea array: " << values.size() << " elements." << endl;
cout << "sizeof(myints): " << sizeof(values) << endl;
}
这里是参考资料:http://www.cplusplus.com/reference/array/array/size/
这是一个非常古老和传奇的问题,已经有很多惊人的答案。但是随着时间的推移,新的功能被添加到语言中,所以我们需要根据可用的新功能不断更新。
我只是注意到还没有人提到c++ 20。所以想写答案。
C + + 20
在c++ 20中,标准库中增加了一种新的更好的方法来查找数组的长度,即std:ssize()。这个函数返回一个带符号的值。
#include <iostream>
int main() {
int arr[] = {1, 2, 3};
std::cout << std::ssize(arr);
return 0;
}
C++17
在c++ 17中,(当时)有一个更好的方法来实现same,即在iterator中定义std::size()。
#include <iostream>
#include <iterator> // required for std::size
int main(){
int arr[] = {1, 2, 3};
std::cout << "Size is " << std::size(arr);
return 0;
}
附注:这个方法也适用于矢量。
Old
这种传统的方法已经在许多其他的答案中提到过。
#include <iostream>
int main() {
int array[] = { 1, 2, 3 };
std::cout << sizeof(array) / sizeof(array[0]);
return 0;
}
仅供参考,如果你想知道为什么这种方法不工作时,数组传递给另一个函数。原因是,
在c++中,数组不是按值传递的,而是传递指向数组的指针。在某些情况下,传递整个数组可能是昂贵的操作。你可以通过将数组传递给某个函数来测试这一点,并对那里的数组进行一些更改,然后再次在main中打印数组。你会得到更新的结果。
正如你已经知道的,sizeof()函数给出了字节数,所以在其他函数中,它将返回分配给指针的字节数,而不是整个数组。所以这种方法行不通。
但我相信你能根据你的要求找到一个好办法。
快乐的编码。
答:
int number_of_elements = sizeof(array)/sizeof(array[0])
解释:
由于编译器为每种类型的数据设置了特定大小的内存块,而数组只是这些数据的一组,因此只需将数组的大小除以数据类型的大小。如果我有一个包含30个字符串的数组,我的系统会为数组的每个元素(字符串)留出24个字节。在30个元素时,总共是720个字节。720/24 == 30个元素。这个小而紧凑的算法是:
Int number_of_elements = sizeof(array)/sizeof(array[0]),这相当于
Number_of_elements = 720/24
请注意,您不需要知道数组是什么数据类型,即使它是自定义数据类型。
而不是使用内置的数组函数aka:
int x[3] = {0, 1, 2};
您应该使用数组类和数组模板。试一试:
#include <array>
array<type_of_the_array, number_of_elements_in_the_array> Name_of_Array = {};
现在如果你想求数组的长度,你所要做的就是使用数组类中的size函数。
Name_of_Array.size();
这应该返回数组中元素的长度。
下面是谷歌Protobuf中ArraySize的一个实现。
#define GOOGLE_ARRAYSIZE(a) \
((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
// test codes...
char* ptr[] = { "you", "are", "here" };
int testarr[] = {1, 2, 3, 4};
cout << GOOGLE_ARRAYSIZE(testarr) << endl;
cout << GOOGLE_ARRAYSIZE(ptr) << endl;
ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in the array) and sizeof(*(arr)) (the # of bytes in one array element). If the former is divisible by the latter, perhaps arr is indeed an array, in which case the division result is the # of elements in the array. Otherwise, arr cannot possibly be an array, and we generate a compiler error to prevent the code from compiling. Since the size of bool is implementation-defined, we need to cast !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final result has type size_t. This macro is not perfect as it wrongfully accepts certain pointers, namely where the pointer size is divisible by the pointee size. Since all our code has to go through a 32-bit compiler, where a pointer is 4 bytes, this means all pointers to a type whose size is 3 or greater than 4 will be (righteously) rejected.