检查 C++ 数组是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3857229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if C++ Array is Null
提问by Luron
how do i do that? well i want to check if the array is empty
我怎么做?好吧,我想检查数组是否为空
回答by James McNellis
An array in C++ cannot be null; only a pointer can be null.
C++ 中的数组不能为空;只有指针可以为空。
To test whether a pointer is null, you simply test whether it compares equal to NULL
or 0
.
要测试指针是否为空,您只需测试它是否等于NULL
或0
。
回答by AnT
Array in C++ cannot be "empty". When you define an array object, you explicitly specify the exact size of the array. That array contains (and always will contain) that exact number of elements you specified in the definition. No more no less. It will never be "empty".
C++ 中的数组不能为“空”。定义数组对象时,您明确指定数组的确切大小。该数组包含(并将始终包含)您在定义中指定的确切数量的元素。不多不少。它永远不会是“空的”。
回答by Kiril Kirov
Actually, when you have an array a[SIZE], you can always check:
实际上,当你有一个数组 a[SIZE] 时,你总是可以检查:
if( NULL == a )
{
/*...*/
}
if( NULL == a )
{
/*...*/
}
But it's not necessary, unless you created a dynamic array (using operator new).
但这不是必需的,除非您创建了一个动态数组(使用运算符 new)。
See the other answers, I won't delete it just because it's accepted now. If other answer is accepted, I'll delete this "answer".
查看其他答案,我不会因为它现在被接受而删除它。如果接受其他答案,我将删除此“答案”。
EDIT(almost 4 years later :) )
编辑(将近 4 年后:))
As I get many down-votes for this, I'd like to clarify: I knowthis is useless and a
will neverbe NULL, but it technically answers the question about the NULL
part.
当我得到很多的向下票对于这一点,我想澄清:我知道这是无用的,a
将永远是空的,但它在技术上回答了关于这个问题NULL
的一部分。
Yes, it does NOTmean, the array is empty, NOTat all. As @JamesMcNellis notes below, arrays cannot be NULL, only pointers.
是的,它确实不是意味着,该数组是空的,不是在所有。正如@JamesMcNellis 在下面指出的那样,数组不能为 NULL,只能为指针。
It could only be useful for dynamically allocated arrays with initialized pointerbefore the allocation.
它仅对在分配之前具有初始化指针的动态分配数组有用。
Anyway, I'll wait for accepting other answer and will delete mine.
无论如何,我会等待接受其他答案并删除我的答案。
回答by Vintharas
You can use either static or "dynamic" arrays. An static array would be something like the following:
您可以使用静态或“动态”数组。静态数组类似于以下内容:
int array[5];
That represents an static array of 5 integer elements. This kind of array cannot be null, it is an array of 5 undefined integers.
这表示一个包含 5 个整数元素的静态数组。这种数组不能为空,它是一个由 5 个未定义整数组成的数组。
A "dynamic" array, on the other hand would be something like this:
另一方面,“动态”数组将是这样的:
int* array = new array[5];
In this case, the pointer-to-int is pointing to an array of 5 elements. This pointer could be null, and you would check this case with a simple if statement:
在这种情况下,指向 int 的指针指向一个包含 5 个元素的数组。这个指针可能为空,你可以用一个简单的 if 语句来检查这种情况:
if (array == 0)
回答by pts
If you are using an STL vector
or list
, you can use the empty
or the size
method to check for emptiness:
如果您使用的是 STLvector
或list
,则可以使用empty
或size
方法来检查是否为空:
std::vector<int> v;
if (v.empty()) cout << "empty\n";
if (v.size() == 0) cout << "empty\n";
std::list<int> l;
if (l.empty()) cout << "empty\n";
if (l.size() == 0) cout << "empty\n";
A regular C++ array (like int a[]') or pointer (like
int* a) doesn't know its size.
常规 C++ 数组(如int a[]') or pointer (like
int* a)不知道其大小。
For arrays declared with size (like int a[42]
as a local or global variable or class member), you can use sizeof(a) / sizeof(a[0])
to get the declared size (42
in the example), which will usually not be 0. Arrays you declare this way are never NULL
.
对于使用 size 声明的数组(如int a[42]
作为局部或全局变量或类成员),您可以使用sizeof(a) / sizeof(a[0])
获取声明的大小(42
在示例中),通常不会是 0。您以这种方式声明的数组永远不会NULL
。