如何获取数组的大小? [英] How to get the size of an Array?
问题描述
在C#中,我使用嵌入到数组的Length属性,我想得到的大小。
如何在C ++中做到这一点?
In C# I use the Length property embedded to the array I'd like to get the size of. How to do that in C++?
推荐答案
C / C ++中的数组不会将它们的长度存储在内存中,它不可能找到它们的大小纯粹给一个指向数组的指针。使用这些语言中的数组的任何代码都依赖于常量已知的大小,或者传递一个指定其大小的单独变量。
Arrays in C/C++ do not store their lengths in memory, so it is impossible to find their size purely given a pointer to an array. Any code using arrays in those languages relies on a constant known size, or a separate variable being passed around that specifies their size.
这是一个常见的解决方案,提出问题,就是使用 std :: vector
类从标准库,它更接近一个托管(C#)数组,即存储它的长度,另外还有一些有用的成员函数(用于搜索和操作)。
A common solution to this, if it does present a problem, is to use the std::vector
class from the standard library, which is much closer to a managed (C#) array, i.e. stores its length and additionally has a few useful member functions (for searching and manipulation).
使用 std :: vector
,您可以简单地调用 vector.size()
长度。
Using std::vector
, you can simply call vector.size()
to get its size/length.
这篇关于如何获取数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!