This question already has an answer here:
这个问题已经有了答案:
- How do I use arrays in C++? 5 answers
- 如何在c++中使用数组?5个回答
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#中,我使用嵌入到我想要的数组中的Length属性。用c++怎么做呢?
5 个解决方案
#1
14
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.
C/ 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类,它更接近于托管(c#)数组,例如,存储它的长度,并添加一些有用的成员函数(用于搜索和操作)。
Using std::vector
, you can simply call vector.size()
to get its size/length.
使用std::vector,您可以简单地调用vector.size()来获取它的大小/长度。
#2
15
It really depends what you mean by "array". Arrays in C++ will have a size (meaning the "raw" byte-size now) that equals to N times the size of one item. By that one can easily get the number of items using the sizeof
operator. But this requires that you still have access to the type of that array. Once you pass it to functions, it will be converted to pointers, and then you are lost. No size can be determined anymore. You will have to construct some other way that relies on the value of the elements to calculate the size.
这真的取决于你所说的“数组”是什么意思。c++中的数组将有一个大小(现在是“原始”字节大小),等于N乘以一个条目的大小。通过使用sizeof运算符可以很容易地得到项目的数量。但这要求您仍然可以访问该数组的类型。一旦将其传递给函数,它将转换为指针,然后就会丢失。再也无法确定大小。您将不得不构造另一种依赖于元素值来计算大小的方法。
Here are some examples:
下面是一些例子:
int a[5];
size_t size = (sizeof a / sizeof a[0]); // size == 5
int *pa = a;
If we now lose the name "a" (and therefor its type), for example by passing "pa" to a function where that function only then has the value of that pointer, then we are out of luck. We then cannot receive the size anymore. We would need to pass the size along with the pointer to that function.
如果我们现在丢失了“a”(及其类型)的名称,例如将“pa”传递给一个函数,该函数只有那个指针的值,那么我们就不走运了。这样我们就不能再收到这个尺寸了。我们需要将大小与指向该函数的指针一起传递。
The same restrictions apply when we get an array by using new
. It returns a pointer pointing to that array's elements, and thus the size will be lost.
当我们使用new获取数组时,同样的限制也适用。它返回指向该数组元素的指针,因此大小将丢失。
int *p = new int[5];
// can't get the size of the array p points to.
delete[] p;
It can't return a pointer that has the type of the array incorporated, because the size of the array created with new
can be calculated at runtime. But types in C++ must be set at compile-time. Thus, new
erases that array part, and returns a pointer to the elements instead. Note that you don't need to mess with new
in C++. You can use the std::vector
template, as recommended by another answer.
它不能返回包含数组类型的指针,因为使用new创建的数组的大小可以在运行时计算。但是c++中的类型必须在编译时设置。因此,new将删除该数组部分,并返回指向元素的指针。注意,您不需要在c++中使用new。您可以使用std::vector模板,根据另一个答案的建议。
#3
5
To count the number of elements in a static array, you can create a template function:
要计算静态数组中的元素数量,可以创建一个模板函数:
template < typename T, size_t N >
size_t countof( T const (&array)[ N ] )
{
return N;
}
For standard containers such as std::vector
, the size()
function is used. This pattern is also used with boost arrays, which are fixed size arrays and claim no worse performance to static arrays. The code you have in a comment above should be:
对于标准容器,如std::vector,使用size()函数。这个模式也被用于boost数组,它是固定大小的数组,并声称对静态数组没有更糟糕的性能。你在上面的评论中的代码应该是:
for ( std::vector::size_type i(0); i < entries.size(); ++i )
( assuming the size changes in the loop, otherwise hoist it, ) rather than treating size as a member variable.
(假设循环中的大小改变,否则提升它)而不是将大小作为成员变量。
#4
0
In C/C++, arrays are simply pointers to the first element in the array, so there is no way to keep track of the size or # of elements. You will have to pass an integer indicating the size of the array if you need to use it.
在C/ c++中,数组只是指向数组中的第一个元素的指针,因此无法跟踪元素的大小或#。如果需要使用数组,则必须传递一个表示数组大小的整数。
Strings may have their length determined, assuming they are null terminated, by using the strlen() function, but that simply counts until the \0 character.
字符串可以通过使用strlen()函数确定它们的长度,假设它们是空终止的,但是这只需要计算到\0字符。
#5
0
Als Nolrodin pointed out above, it is pretty much impossible to get the size of an plain array in C++ if you only have a pointer to its first element. However if you have a fixed-size array there is a well-known C trick to work out the number of elements in the array at compile time, namely by doing:
在上面,Als Nolrodin指出,如果您只有一个指向它的第一个元素的指针,那么在c++中几乎不可能得到一个普通数组的大小。但是,如果您有一个固定大小的数组,那么有一个众所周知的C技巧可以在编译时计算数组中的元素数量,即:
GrmblFx loadsOfElements[1027];
GrmblFx_length = sizeof(loadsOfElements)/sizeof(GrmblFx);
#1
14
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.
C/ 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类,它更接近于托管(c#)数组,例如,存储它的长度,并添加一些有用的成员函数(用于搜索和操作)。
Using std::vector
, you can simply call vector.size()
to get its size/length.
使用std::vector,您可以简单地调用vector.size()来获取它的大小/长度。
#2
15
It really depends what you mean by "array". Arrays in C++ will have a size (meaning the "raw" byte-size now) that equals to N times the size of one item. By that one can easily get the number of items using the sizeof
operator. But this requires that you still have access to the type of that array. Once you pass it to functions, it will be converted to pointers, and then you are lost. No size can be determined anymore. You will have to construct some other way that relies on the value of the elements to calculate the size.
这真的取决于你所说的“数组”是什么意思。c++中的数组将有一个大小(现在是“原始”字节大小),等于N乘以一个条目的大小。通过使用sizeof运算符可以很容易地得到项目的数量。但这要求您仍然可以访问该数组的类型。一旦将其传递给函数,它将转换为指针,然后就会丢失。再也无法确定大小。您将不得不构造另一种依赖于元素值来计算大小的方法。
Here are some examples:
下面是一些例子:
int a[5];
size_t size = (sizeof a / sizeof a[0]); // size == 5
int *pa = a;
If we now lose the name "a" (and therefor its type), for example by passing "pa" to a function where that function only then has the value of that pointer, then we are out of luck. We then cannot receive the size anymore. We would need to pass the size along with the pointer to that function.
如果我们现在丢失了“a”(及其类型)的名称,例如将“pa”传递给一个函数,该函数只有那个指针的值,那么我们就不走运了。这样我们就不能再收到这个尺寸了。我们需要将大小与指向该函数的指针一起传递。
The same restrictions apply when we get an array by using new
. It returns a pointer pointing to that array's elements, and thus the size will be lost.
当我们使用new获取数组时,同样的限制也适用。它返回指向该数组元素的指针,因此大小将丢失。
int *p = new int[5];
// can't get the size of the array p points to.
delete[] p;
It can't return a pointer that has the type of the array incorporated, because the size of the array created with new
can be calculated at runtime. But types in C++ must be set at compile-time. Thus, new
erases that array part, and returns a pointer to the elements instead. Note that you don't need to mess with new
in C++. You can use the std::vector
template, as recommended by another answer.
它不能返回包含数组类型的指针,因为使用new创建的数组的大小可以在运行时计算。但是c++中的类型必须在编译时设置。因此,new将删除该数组部分,并返回指向元素的指针。注意,您不需要在c++中使用new。您可以使用std::vector模板,根据另一个答案的建议。
#3
5
To count the number of elements in a static array, you can create a template function:
要计算静态数组中的元素数量,可以创建一个模板函数:
template < typename T, size_t N >
size_t countof( T const (&array)[ N ] )
{
return N;
}
For standard containers such as std::vector
, the size()
function is used. This pattern is also used with boost arrays, which are fixed size arrays and claim no worse performance to static arrays. The code you have in a comment above should be:
对于标准容器,如std::vector,使用size()函数。这个模式也被用于boost数组,它是固定大小的数组,并声称对静态数组没有更糟糕的性能。你在上面的评论中的代码应该是:
for ( std::vector::size_type i(0); i < entries.size(); ++i )
( assuming the size changes in the loop, otherwise hoist it, ) rather than treating size as a member variable.
(假设循环中的大小改变,否则提升它)而不是将大小作为成员变量。
#4
0
In C/C++, arrays are simply pointers to the first element in the array, so there is no way to keep track of the size or # of elements. You will have to pass an integer indicating the size of the array if you need to use it.
在C/ c++中,数组只是指向数组中的第一个元素的指针,因此无法跟踪元素的大小或#。如果需要使用数组,则必须传递一个表示数组大小的整数。
Strings may have their length determined, assuming they are null terminated, by using the strlen() function, but that simply counts until the \0 character.
字符串可以通过使用strlen()函数确定它们的长度,假设它们是空终止的,但是这只需要计算到\0字符。
#5
0
Als Nolrodin pointed out above, it is pretty much impossible to get the size of an plain array in C++ if you only have a pointer to its first element. However if you have a fixed-size array there is a well-known C trick to work out the number of elements in the array at compile time, namely by doing:
在上面,Als Nolrodin指出,如果您只有一个指向它的第一个元素的指针,那么在c++中几乎不可能得到一个普通数组的大小。但是,如果您有一个固定大小的数组,那么有一个众所周知的C技巧可以在编译时计算数组中的元素数量,即:
GrmblFx loadsOfElements[1027];
GrmblFx_length = sizeof(loadsOfElements)/sizeof(GrmblFx);