有没有办法找出一个数组有多少个值?检测是否已经到达数组的末尾也可以工作。
当前回答
有没有办法找出一个数组有多少个值?
Yes!
尝试sizeof(数组)/ sizeof(阵列[0])
检测是否已经到达数组的末尾也可以工作。
我看不到任何方法,除非你的数组是一个字符数组(即字符串)。
注:在c++中总是使用std::vector。有几个内置函数和一个扩展功能。
其他回答
从c++ 11开始,引入了一些新的模板来帮助减少处理数组长度时的痛苦。它们都定义在header <type_traits>中。
std::rank<T>::value If T is an array type, provides the member constant value equal to the number of dimensions of the array. For any other type, value is 0. std::extent<T, N>::value If T is an array type, provides the member constant value equal to the number of elements along the Nth dimension of the array, if N is in [0, std::rank<T>::value). For any other type, or if T is array of unknown bound along its first dimension and N is 0, value is 0. std::remove_extent<T>::type If T is an array of some type X, provides the member typedef type equal to X, otherwise type is T. Note that if T is a multidimensional array, only the first dimension is removed. std::remove_all_extents<T>::type If T is a multidimensional array of some type X, provides the member typedef type equal to X, otherwise type is T.
要获得多维数组的任何维度上的长度,可以使用decltype与std::extent结合使用。例如:
#include <iostream>
#include <type_traits> // std::remove_extent std::remove_all_extents std::rank std::extent
template<class T, size_t N>
constexpr size_t length(T(&)[N]) { return N; }
template<class T, size_t N>
constexpr size_t length2(T(&arr)[N]) { return sizeof(arr) / sizeof(*arr); }
int main()
{
int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
// New way
constexpr auto l1 = std::extent<decltype(a)>::value; // 5
constexpr auto l2 = std::extent<decltype(a), 1>::value; // 4
constexpr auto l3 = std::extent<decltype(a), 2>::value; // 3
constexpr auto l4 = std::extent<decltype(a), 3>::value; // 0
// Mixed way
constexpr auto la = length(a);
//constexpr auto lpa = length(*a); // compile error
//auto lpa = length(*a); // get at runtime
std::remove_extent<decltype(a)>::type pa; // get at compile time
//std::remove_reference<decltype(*a)>::type pa; // same as above
constexpr auto lpa = length(pa);
std::cout << la << ' ' << lpa << '\n';
// Old way
constexpr auto la2 = sizeof(a) / sizeof(*a);
constexpr auto lpa2 = sizeof(*a) / sizeof(**a);
std::cout << la2 << ' ' << lpa2 << '\n';
return 0;
}
BTY,获取多维数组中元素的总数:
constexpr auto l = sizeof(a) / sizeof(std::remove_all_extents<decltype(a)>::type);
或者把它放在一个函数模板中:
#include <iostream>
#include <type_traits>
template<class T>
constexpr size_t len(T &a)
{
return sizeof(a) / sizeof(typename std::remove_all_extents<T>::type);
}
int main()
{
int a[5][4][3]{{{1,2,3}, {4,5,6}}, { }, {{7,8,9}}};
constexpr auto ttt = len(a);
int i;
std::cout << ttt << ' ' << len(i) << '\n';
return 0;
}
更多如何使用它们的例子可以通过以下链接找到。
对于旧的g++编译器,您可以这样做
template <class T, size_t N>
char (&helper(T (&)[N]))[N];
#define arraysize(array) (sizeof(helper(array)))
int main() {
int a[10];
std::cout << arraysize(a) << std::endl;
return 0;
}
对于c++ /CX(在Visual Studio中使用c++编写UWP应用程序时),我们可以通过简单地使用size()函数来查找数组中值的数量。
源代码:
string myArray[] = { "Example1", "Example2", "Example3", "Example4" };
int size_of_array=size(myArray);
如果你计算size_of_array的输出将是:
>>> 4
Sizeof (array_name)给出整个数组的大小,Sizeof (int)给出每个数组元素的数据类型的大小。
所以用整个数组的大小除以数组中单个元素的大小就得到了数组的长度。
int array_name[] = {1, 2, 3, 4, 5, 6};
int length = sizeof(array_name)/sizeof(int);
最常见的原因之一是,您希望将数组传递给函数,而不必为其大小传递另一个参数。您通常也希望数组大小是动态的。该数组可能包含对象,而不是原语,而且对象可能很复杂,因此size_of()是计算计数的不安全选项。
正如其他人建议的那样,考虑使用std::vector或list等来代替原始数组。然而,在旧的编译器上,您仍然不能通过简单地这样做得到您可能想要的最终解决方案,因为填充容器需要大量丑陋的push_back()行。如果你像我一样,想要一个包含匿名对象的单行解决方案。
如果你使用STL容器替代原始数组,那么这篇SO文章可能会对你有用,让你知道如何初始化它: 用硬编码的元素初始化std::vector最简单的方法是什么?
下面是我使用的一个方法,它将在编译器和平台上普遍工作:
为对象集合创建一个结构体或类作为容器。为<<定义运算符重载函数。
class MyObject;
struct MyObjectList
{
std::list<MyObject> objects;
MyObjectList& operator<<( const MyObject o )
{
objects.push_back( o );
return *this;
}
};
你可以创建以你的结构体作为参数的函数,例如:
someFunc( MyObjectList &objects );
然后,你可以调用这个函数,像这样:
someFunc( MyObjectList() << MyObject(1) << MyObject(2) << MyObject(3) );
这样,您就可以在一行代码中构建并将动态大小的对象集合传递给函数!